OSDN Git Service

am 61d9df3e: wpa_supplicant: Update to 29-Aug-2012 TOT
[android-x86/external-wpa_supplicant_8.git] / src / p2p / p2p.c
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
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
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
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
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
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
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
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
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
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
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
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
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
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  */
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  */
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  */
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
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
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
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  */
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
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
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
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
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
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
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
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
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
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
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
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
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
1150 void p2p_stop_find(struct p2p_data *p2p)
1151 {
1152         p2p_stop_find_for_freq(p2p, 0);
1153 }
1154
1155
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                         os_memcpy(&p2p->channels, &p2p->cfg->channels,
1189                                 sizeof(struct p2p_channels));
1190                 } else {
1191                         p2p->channels.reg_classes = 1;
1192                         p2p->channels.reg_class[0].channels = 1;
1193                         p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1194                         p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
1195                 }
1196 #endif
1197         } else {
1198                 u8 op_reg_class, op_channel;
1199
1200                 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1201                     p2p_supported_freq(p2p, p2p->best_freq_overall) &&
1202                     p2p_freq_to_channel(p2p->cfg->country,
1203                                         p2p->best_freq_overall,
1204                                         &op_reg_class, &op_channel) == 0) {
1205                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1206                                 "P2P: Select best overall channel as "
1207                                 "operating channel preference");
1208                         p2p->op_reg_class = op_reg_class;
1209                         p2p->op_channel = op_channel;
1210                 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1211                            p2p_supported_freq(p2p, p2p->best_freq_5) &&
1212                            p2p_freq_to_channel(p2p->cfg->country,
1213                                                p2p->best_freq_5,
1214                                                &op_reg_class, &op_channel) ==
1215                            0) {
1216                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1217                                 "P2P: Select best 5 GHz channel as "
1218                                 "operating channel preference");
1219                         p2p->op_reg_class = op_reg_class;
1220                         p2p->op_channel = op_channel;
1221                 } else if (!p2p->cfg->cfg_op_channel &&
1222                            p2p->best_freq_24 > 0 &&
1223                            p2p_supported_freq(p2p, p2p->best_freq_24) &&
1224                            p2p_freq_to_channel(p2p->cfg->country,
1225                                                p2p->best_freq_24,
1226                                                &op_reg_class, &op_channel) ==
1227                            0) {
1228                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1229                                 "P2P: Select best 2.4 GHz channel as "
1230                                 "operating channel preference");
1231                         p2p->op_reg_class = op_reg_class;
1232                         p2p->op_channel = op_channel;
1233                 } else {
1234                         p2p->op_reg_class = p2p->cfg->op_reg_class;
1235                         p2p->op_channel = p2p->cfg->op_channel;
1236                 }
1237
1238                 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1239                           sizeof(struct p2p_channels));
1240         }
1241         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1242                 "P2P: Own preference for operation channel: "
1243                 "Operating Class %u Channel %u%s",
1244                 p2p->op_reg_class, p2p->op_channel,
1245                 force_freq ? " (forced)" : "");
1246
1247         return 0;
1248 }
1249
1250
1251 static void p2p_set_dev_persistent(struct p2p_device *dev,
1252                                    int persistent_group)
1253 {
1254         switch (persistent_group) {
1255         case 0:
1256                 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1257                                 P2P_DEV_PREFER_PERSISTENT_RECONN);
1258                 break;
1259         case 1:
1260                 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1261                 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1262                 break;
1263         case 2:
1264                 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1265                         P2P_DEV_PREFER_PERSISTENT_RECONN;
1266                 break;
1267         }
1268 }
1269
1270
1271 int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1272                 enum p2p_wps_method wps_method,
1273                 int go_intent, const u8 *own_interface_addr,
1274                 unsigned int force_freq, int persistent_group,
1275                 const u8 *force_ssid, size_t force_ssid_len,
1276                 int pd_before_go_neg)
1277 {
1278         struct p2p_device *dev;
1279
1280         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1281                 "P2P: Request to start group negotiation - peer=" MACSTR
1282                 "  GO Intent=%d  Intended Interface Address=" MACSTR
1283                 " wps_method=%d persistent_group=%d pd_before_go_neg=%d",
1284                 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1285                 wps_method, persistent_group, pd_before_go_neg);
1286
1287         if (p2p_prepare_channel(p2p, force_freq) < 0)
1288                 return -1;
1289
1290         dev = p2p_get_device(p2p, peer_addr);
1291         if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
1292                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1293                         "P2P: Cannot connect to unknown P2P Device " MACSTR,
1294                         MAC2STR(peer_addr));
1295                 return -1;
1296         }
1297
1298         if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1299                 if (!(dev->info.dev_capab &
1300                       P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
1301                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1302                                 "P2P: Cannot connect to P2P Device " MACSTR
1303                                 " that is in a group and is not discoverable",
1304                                 MAC2STR(peer_addr));
1305                         return -1;
1306                 }
1307                 if (dev->oper_freq <= 0) {
1308                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1309                                 "P2P: Cannot connect to P2P Device " MACSTR
1310                                 " with incomplete information",
1311                                 MAC2STR(peer_addr));
1312                         return -1;
1313                 }
1314
1315                 /*
1316                  * First, try to connect directly. If the peer does not
1317                  * acknowledge frames, assume it is sleeping and use device
1318                  * discoverability via the GO at that point.
1319                  */
1320         }
1321
1322         p2p->ssid_set = 0;
1323         if (force_ssid) {
1324                 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1325                                   force_ssid, force_ssid_len);
1326                 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1327                 p2p->ssid_len = force_ssid_len;
1328                 p2p->ssid_set = 1;
1329         }
1330
1331         dev->flags &= ~P2P_DEV_NOT_YET_READY;
1332         dev->flags &= ~P2P_DEV_USER_REJECTED;
1333         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1334         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1335         if (pd_before_go_neg)
1336                 dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
1337         else
1338                 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
1339         dev->connect_reqs = 0;
1340         dev->go_neg_req_sent = 0;
1341         dev->go_state = UNKNOWN_GO;
1342         p2p_set_dev_persistent(dev, persistent_group);
1343         p2p->go_intent = go_intent;
1344         os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1345
1346         if (p2p->state != P2P_IDLE)
1347                 p2p_stop_find(p2p);
1348
1349         if (p2p->after_scan_tx) {
1350                 /*
1351                  * We need to drop the pending frame to avoid issues with the
1352                  * new GO Negotiation, e.g., when the pending frame was from a
1353                  * previous attempt at starting a GO Negotiation.
1354                  */
1355                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
1356                         "previous pending Action frame TX that was waiting "
1357                         "for p2p_scan completion");
1358                 os_free(p2p->after_scan_tx);
1359                 p2p->after_scan_tx = NULL;
1360         }
1361
1362         dev->wps_method = wps_method;
1363         dev->status = P2P_SC_SUCCESS;
1364
1365         if (force_freq)
1366                 dev->flags |= P2P_DEV_FORCE_FREQ;
1367         else
1368                 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1369
1370         if (p2p->p2p_scan_running) {
1371                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1372                         "P2P: p2p_scan running - delay connect send");
1373                 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1374                 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1375                 return 0;
1376         }
1377         p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1378
1379         return p2p_connect_send(p2p, dev);
1380 }
1381
1382
1383 int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1384                   enum p2p_wps_method wps_method,
1385                   int go_intent, const u8 *own_interface_addr,
1386                   unsigned int force_freq, int persistent_group,
1387                   const u8 *force_ssid, size_t force_ssid_len)
1388 {
1389         struct p2p_device *dev;
1390
1391         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1392                 "P2P: Request to authorize group negotiation - peer=" MACSTR
1393                 "  GO Intent=%d  Intended Interface Address=" MACSTR
1394                 " wps_method=%d  persistent_group=%d",
1395                 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1396                 wps_method, persistent_group);
1397
1398         if (p2p_prepare_channel(p2p, force_freq) < 0)
1399                 return -1;
1400
1401         dev = p2p_get_device(p2p, peer_addr);
1402         if (dev == NULL) {
1403                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1404                         "P2P: Cannot authorize unknown P2P Device " MACSTR,
1405                         MAC2STR(peer_addr));
1406                 return -1;
1407         }
1408
1409         p2p->ssid_set = 0;
1410         if (force_ssid) {
1411                 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1412                                   force_ssid, force_ssid_len);
1413                 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1414                 p2p->ssid_len = force_ssid_len;
1415                 p2p->ssid_set = 1;
1416         }
1417
1418         dev->flags &= ~P2P_DEV_NOT_YET_READY;
1419         dev->flags &= ~P2P_DEV_USER_REJECTED;
1420         dev->go_neg_req_sent = 0;
1421         dev->go_state = UNKNOWN_GO;
1422         p2p_set_dev_persistent(dev, persistent_group);
1423         p2p->go_intent = go_intent;
1424         os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1425
1426         dev->wps_method = wps_method;
1427         dev->status = P2P_SC_SUCCESS;
1428
1429         if (force_freq)
1430                 dev->flags |= P2P_DEV_FORCE_FREQ;
1431         else
1432                 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1433
1434         return 0;
1435 }
1436
1437
1438 void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1439                       struct p2p_device *dev, struct p2p_message *msg)
1440 {
1441         os_get_time(&dev->last_seen);
1442
1443         p2p_copy_wps_info(dev, 0, msg);
1444
1445         if (msg->listen_channel) {
1446                 int freq;
1447                 freq = p2p_channel_to_freq((char *) msg->listen_channel,
1448                                            msg->listen_channel[3],
1449                                            msg->listen_channel[4]);
1450                 if (freq < 0) {
1451                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1452                                 "P2P: Unknown peer Listen channel: "
1453                                 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1454                                 msg->listen_channel[0],
1455                                 msg->listen_channel[1],
1456                                 msg->listen_channel[2],
1457                                 msg->listen_channel[3],
1458                                 msg->listen_channel[4]);
1459                 } else {
1460                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
1461                                 "peer " MACSTR " Listen channel: %u -> %u MHz",
1462                                 MAC2STR(dev->info.p2p_device_addr),
1463                                 dev->listen_freq, freq);
1464                         dev->listen_freq = freq;
1465                 }
1466         }
1467
1468         if (msg->wfd_subelems) {
1469                 wpabuf_free(dev->info.wfd_subelems);
1470                 dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
1471         }
1472
1473         if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1474                 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
1475                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1476                         "P2P: Completed device entry based on data from "
1477                         "GO Negotiation Request");
1478         } else {
1479                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1480                         "P2P: Created device entry based on GO Neg Req: "
1481                         MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1482                         "listen_freq=%d",
1483                         MAC2STR(dev->info.p2p_device_addr),
1484                         dev->info.dev_capab, dev->info.group_capab,
1485                         dev->info.device_name, dev->listen_freq);
1486         }
1487
1488         dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1489
1490         if (dev->flags & P2P_DEV_USER_REJECTED) {
1491                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1492                         "P2P: Do not report rejected device");
1493                 return;
1494         }
1495
1496         p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1497                             !(dev->flags & P2P_DEV_REPORTED_ONCE));
1498         dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1499 }
1500
1501
1502 void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1503 {
1504         os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1505         p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1506         os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1507                   p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1508         *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1509 }
1510
1511
1512 int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1513 {
1514         p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
1515         p2p_random(params->passphrase, 8);
1516         return 0;
1517 }
1518
1519
1520 void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1521 {
1522         struct p2p_go_neg_results res;
1523         int go = peer->go_state == LOCAL_GO;
1524         struct p2p_channels intersection;
1525         int freqs;
1526         size_t i, j;
1527
1528         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1529                 "P2P: GO Negotiation with " MACSTR " completed (%s will be "
1530                 "GO)", MAC2STR(peer->info.p2p_device_addr),
1531                 go ? "local end" : "peer");
1532
1533         os_memset(&res, 0, sizeof(res));
1534         res.role_go = go;
1535         os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1536         os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1537         res.wps_method = peer->wps_method;
1538         if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1539                 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1540                         res.persistent_group = 2;
1541                 else
1542                         res.persistent_group = 1;
1543         }
1544
1545         if (go) {
1546                 /* Setup AP mode for WPS provisioning */
1547                 res.freq = p2p_channel_to_freq(p2p->cfg->country,
1548                                                p2p->op_reg_class,
1549                                                p2p->op_channel);
1550                 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1551                 res.ssid_len = p2p->ssid_len;
1552                 p2p_random(res.passphrase, 8);
1553         } else {
1554                 res.freq = peer->oper_freq;
1555                 if (p2p->ssid_len) {
1556                         os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1557                         res.ssid_len = p2p->ssid_len;
1558                 }
1559         }
1560
1561         p2p_channels_intersect(&p2p->channels, &peer->channels,
1562                                &intersection);
1563         freqs = 0;
1564         for (i = 0; i < intersection.reg_classes; i++) {
1565                 struct p2p_reg_class *c = &intersection.reg_class[i];
1566                 if (freqs + 1 == P2P_MAX_CHANNELS)
1567                         break;
1568                 for (j = 0; j < c->channels; j++) {
1569                         int freq;
1570                         if (freqs + 1 == P2P_MAX_CHANNELS)
1571                                 break;
1572                         freq = p2p_channel_to_freq(peer->country, c->reg_class,
1573                                                    c->channel[j]);
1574                         if (freq < 0)
1575                                 continue;
1576                         res.freq_list[freqs++] = freq;
1577                 }
1578         }
1579
1580         res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1581
1582         p2p_clear_timeout(p2p);
1583         p2p->ssid_set = 0;
1584         peer->go_neg_req_sent = 0;
1585         peer->wps_method = WPS_NOT_READY;
1586
1587         p2p_set_state(p2p, P2P_PROVISIONING);
1588         p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1589 }
1590
1591
1592 static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1593                               const u8 *data, size_t len, int rx_freq)
1594 {
1595         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1596                 "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
1597         wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1598
1599         if (len < 1)
1600                 return;
1601
1602         switch (data[0]) {
1603         case P2P_GO_NEG_REQ:
1604                 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1605                 break;
1606         case P2P_GO_NEG_RESP:
1607                 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1608                 break;
1609         case P2P_GO_NEG_CONF:
1610                 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1611                 break;
1612         case P2P_INVITATION_REQ:
1613                 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1614                                            rx_freq);
1615                 break;
1616         case P2P_INVITATION_RESP:
1617                 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1618                 break;
1619         case P2P_PROV_DISC_REQ:
1620                 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1621                 break;
1622         case P2P_PROV_DISC_RESP:
1623                 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1624                 break;
1625         case P2P_DEV_DISC_REQ:
1626                 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1627                 break;
1628         case P2P_DEV_DISC_RESP:
1629                 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1630                 break;
1631         default:
1632                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1633                         "P2P: Unsupported P2P Public Action frame type %d",
1634                         data[0]);
1635                 break;
1636         }
1637 }
1638
1639
1640 static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1641                                  const u8 *sa, const u8 *bssid, const u8 *data,
1642                                  size_t len, int freq)
1643 {
1644         if (len < 1)
1645                 return;
1646
1647         switch (data[0]) {
1648         case WLAN_PA_VENDOR_SPECIFIC:
1649                 data++;
1650                 len--;
1651                 if (len < 3)
1652                         return;
1653                 if (WPA_GET_BE24(data) != OUI_WFA)
1654                         return;
1655
1656                 data += 3;
1657                 len -= 3;
1658                 if (len < 1)
1659                         return;
1660
1661                 if (*data != P2P_OUI_TYPE)
1662                         return;
1663
1664                 p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
1665                 break;
1666         case WLAN_PA_GAS_INITIAL_REQ:
1667                 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1668                 break;
1669         case WLAN_PA_GAS_INITIAL_RESP:
1670                 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1671                 break;
1672         case WLAN_PA_GAS_COMEBACK_REQ:
1673                 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1674                 break;
1675         case WLAN_PA_GAS_COMEBACK_RESP:
1676                 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1677                 break;
1678         }
1679 }
1680
1681
1682 void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1683                    const u8 *bssid, u8 category,
1684                    const u8 *data, size_t len, int freq)
1685 {
1686         if (category == WLAN_ACTION_PUBLIC) {
1687                 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1688                 return;
1689         }
1690
1691         if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1692                 return;
1693
1694         if (len < 4)
1695                 return;
1696
1697         if (WPA_GET_BE24(data) != OUI_WFA)
1698                 return;
1699         data += 3;
1700         len -= 3;
1701
1702         if (*data != P2P_OUI_TYPE)
1703                 return;
1704         data++;
1705         len--;
1706
1707         /* P2P action frame */
1708         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1709                 "P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
1710         wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1711
1712         if (len < 1)
1713                 return;
1714         switch (data[0]) {
1715         case P2P_NOA:
1716                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1717                         "P2P: Received P2P Action - Notice of Absence");
1718                 /* TODO */
1719                 break;
1720         case P2P_PRESENCE_REQ:
1721                 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1722                 break;
1723         case P2P_PRESENCE_RESP:
1724                 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1725                 break;
1726         case P2P_GO_DISC_REQ:
1727                 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1728                 break;
1729         default:
1730                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1731                         "P2P: Received P2P Action - unknown type %u", data[0]);
1732                 break;
1733         }
1734 }
1735
1736
1737 static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1738 {
1739         struct p2p_data *p2p = eloop_ctx;
1740         if (p2p->go_neg_peer == NULL)
1741                 return;
1742         p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1743         p2p->go_neg_peer->status = P2P_SC_SUCCESS;
1744         p2p_connect_send(p2p, p2p->go_neg_peer);
1745 }
1746
1747
1748 static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
1749 {
1750         struct p2p_data *p2p = eloop_ctx;
1751         if (p2p->invite_peer == NULL)
1752                 return;
1753         p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1754         p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
1755 }
1756
1757
1758 static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
1759                                        const u8 *ie, size_t ie_len)
1760 {
1761         struct p2p_message msg;
1762         struct p2p_device *dev;
1763
1764         os_memset(&msg, 0, sizeof(msg));
1765         if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
1766         {
1767                 p2p_parse_free(&msg);
1768                 return; /* not a P2P probe */
1769         }
1770
1771         if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
1772             os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
1773             != 0) {
1774                 /* The Probe Request is not part of P2P Device Discovery. It is
1775                  * not known whether the source address of the frame is the P2P
1776                  * Device Address or P2P Interface Address. Do not add a new
1777                  * peer entry based on this frames.
1778                  */
1779                 p2p_parse_free(&msg);
1780                 return;
1781         }
1782
1783         dev = p2p_get_device(p2p, addr);
1784         if (dev) {
1785                 if (dev->country[0] == 0 && msg.listen_channel)
1786                         os_memcpy(dev->country, msg.listen_channel, 3);
1787                 os_get_time(&dev->last_seen);
1788                 p2p_parse_free(&msg);
1789                 return; /* already known */
1790         }
1791
1792         dev = p2p_create_device(p2p, addr);
1793         if (dev == NULL) {
1794                 p2p_parse_free(&msg);
1795                 return;
1796         }
1797
1798         os_get_time(&dev->last_seen);
1799         dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
1800
1801         if (msg.listen_channel) {
1802                 os_memcpy(dev->country, msg.listen_channel, 3);
1803                 dev->listen_freq = p2p_channel_to_freq(dev->country,
1804                                                        msg.listen_channel[3],
1805                                                        msg.listen_channel[4]);
1806         }
1807
1808         p2p_copy_wps_info(dev, 1, &msg);
1809
1810         if (msg.wfd_subelems) {
1811                 wpabuf_free(dev->info.wfd_subelems);
1812                 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
1813         }
1814
1815         p2p_parse_free(&msg);
1816
1817         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1818                 "P2P: Created device entry based on Probe Req: " MACSTR
1819                 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1820                 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
1821                 dev->info.group_capab, dev->info.device_name,
1822                 dev->listen_freq);
1823 }
1824
1825
1826 struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
1827                                                 const u8 *addr,
1828                                                 struct p2p_message *msg)
1829 {
1830         struct p2p_device *dev;
1831
1832         dev = p2p_get_device(p2p, addr);
1833         if (dev) {
1834                 os_get_time(&dev->last_seen);
1835                 return dev; /* already known */
1836         }
1837
1838         dev = p2p_create_device(p2p, addr);
1839         if (dev == NULL)
1840                 return NULL;
1841
1842         p2p_add_dev_info(p2p, addr, dev, msg);
1843
1844         return dev;
1845 }
1846
1847
1848 static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
1849 {
1850         if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
1851                 return 1;
1852         if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
1853             WPA_GET_BE32(&req_dev_type[2]) == 0 &&
1854             WPA_GET_BE16(&req_dev_type[6]) == 0)
1855                 return 1; /* Category match with wildcard OUI/sub-category */
1856         return 0;
1857 }
1858
1859
1860 int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
1861                         size_t num_req_dev_type)
1862 {
1863         size_t i;
1864         for (i = 0; i < num_req_dev_type; i++) {
1865                 if (dev_type_match(dev_type, req_dev_type[i]))
1866                         return 1;
1867         }
1868         return 0;
1869 }
1870
1871
1872 /**
1873  * p2p_match_dev_type - Match local device type with requested type
1874  * @p2p: P2P module context from p2p_init()
1875  * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1876  * Returns: 1 on match, 0 on mismatch
1877  *
1878  * This function can be used to match the Requested Device Type attribute in
1879  * WPS IE with the local device types for deciding whether to reply to a Probe
1880  * Request frame.
1881  */
1882 int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
1883 {
1884         struct wps_parse_attr attr;
1885         size_t i;
1886
1887         if (wps_parse_msg(wps, &attr))
1888                 return 1; /* assume no Requested Device Type attributes */
1889
1890         if (attr.num_req_dev_type == 0)
1891                 return 1; /* no Requested Device Type attributes -> match */
1892
1893         if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
1894                                 attr.num_req_dev_type))
1895                 return 1; /* Own Primary Device Type matches */
1896
1897         for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
1898                 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
1899                                         attr.req_dev_type,
1900                                         attr.num_req_dev_type))
1901                 return 1; /* Own Secondary Device Type matches */
1902
1903         /* No matching device type found */
1904         return 0;
1905 }
1906
1907
1908 struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
1909 {
1910         struct wpabuf *buf;
1911         u8 *len;
1912         int pw_id = -1;
1913         size_t extra = 0;
1914
1915 #ifdef CONFIG_WIFI_DISPLAY
1916         if (p2p->wfd_ie_probe_resp)
1917                 extra = wpabuf_len(p2p->wfd_ie_probe_resp);
1918 #endif /* CONFIG_WIFI_DISPLAY */
1919
1920         buf = wpabuf_alloc(1000 + extra);
1921         if (buf == NULL)
1922                 return NULL;
1923
1924         if (p2p->go_neg_peer) {
1925                 /* Advertise immediate availability of WPS credential */
1926                 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
1927         }
1928
1929         p2p_build_wps_ie(p2p, buf, pw_id, 1);
1930
1931 #ifdef CONFIG_WIFI_DISPLAY
1932         if (p2p->wfd_ie_probe_resp)
1933                 wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
1934 #endif /* CONFIG_WIFI_DISPLAY */
1935
1936         /* P2P IE */
1937         len = p2p_buf_add_ie_hdr(buf);
1938         p2p_buf_add_capability(buf, p2p->dev_capab &
1939                                ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
1940         if (p2p->ext_listen_interval)
1941                 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
1942                                               p2p->ext_listen_interval);
1943         p2p_buf_add_device_info(buf, p2p, NULL);
1944         p2p_buf_update_ie_hdr(buf, len);
1945
1946         return buf;
1947 }
1948
1949
1950 static int is_11b(u8 rate)
1951 {
1952         return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16;
1953 }
1954
1955
1956 static int supp_rates_11b_only(struct ieee802_11_elems *elems)
1957 {
1958         int num_11b = 0, num_others = 0;
1959         int i;
1960
1961         if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL)
1962                 return 0;
1963
1964         for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) {
1965                 if (is_11b(elems->supp_rates[i]))
1966                         num_11b++;
1967                 else
1968                         num_others++;
1969         }
1970
1971         for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len;
1972              i++) {
1973                 if (is_11b(elems->ext_supp_rates[i]))
1974                         num_11b++;
1975                 else
1976                         num_others++;
1977         }
1978
1979         return num_11b > 0 && num_others == 0;
1980 }
1981
1982
1983 static enum p2p_probe_req_status
1984 p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
1985                 const u8 *bssid, const u8 *ie, size_t ie_len)
1986 {
1987         struct ieee802_11_elems elems;
1988         struct wpabuf *buf;
1989         struct ieee80211_mgmt *resp;
1990         struct p2p_message msg;
1991         struct wpabuf *ies;
1992
1993         if (!p2p->in_listen || !p2p->drv_in_listen) {
1994                 /* not in Listen state - ignore Probe Request */
1995                 return P2P_PREQ_NOT_LISTEN;
1996         }
1997
1998         if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
1999             ParseFailed) {
2000                 /* Ignore invalid Probe Request frames */
2001                 return P2P_PREQ_MALFORMED;
2002         }
2003
2004         if (elems.p2p == NULL) {
2005                 /* not a P2P probe - ignore it */
2006                 return P2P_PREQ_NOT_P2P;
2007         }
2008
2009         if (dst && !is_broadcast_ether_addr(dst) &&
2010             os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
2011                 /* Not sent to the broadcast address or our P2P Device Address
2012                  */
2013                 return P2P_PREQ_NOT_PROCESSED;
2014         }
2015
2016         if (bssid && !is_broadcast_ether_addr(bssid)) {
2017                 /* Not sent to the Wildcard BSSID */
2018                 return P2P_PREQ_NOT_PROCESSED;
2019         }
2020
2021         if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
2022             os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
2023             0) {
2024                 /* not using P2P Wildcard SSID - ignore */
2025                 return P2P_PREQ_NOT_PROCESSED;
2026         }
2027
2028         if (supp_rates_11b_only(&elems)) {
2029                 /* Indicates support for 11b rates only */
2030                 return P2P_PREQ_NOT_P2P;
2031         }
2032
2033         os_memset(&msg, 0, sizeof(msg));
2034         if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
2035                 /* Could not parse P2P attributes */
2036                 return P2P_PREQ_NOT_P2P;
2037         }
2038
2039         if (msg.device_id &&
2040             os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
2041                 /* Device ID did not match */
2042                 p2p_parse_free(&msg);
2043                 return P2P_PREQ_NOT_PROCESSED;
2044         }
2045
2046         /* Check Requested Device Type match */
2047         if (msg.wps_attributes &&
2048             !p2p_match_dev_type(p2p, msg.wps_attributes)) {
2049                 /* No match with Requested Device Type */
2050                 p2p_parse_free(&msg);
2051                 return P2P_PREQ_NOT_PROCESSED;
2052         }
2053         p2p_parse_free(&msg);
2054
2055         if (!p2p->cfg->send_probe_resp) {
2056                 /* Response generated elsewhere */
2057                 return P2P_PREQ_NOT_PROCESSED;
2058         }
2059
2060         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2061                 "P2P: Reply to P2P Probe Request in Listen state");
2062
2063         /*
2064          * We do not really have a specific BSS that this frame is advertising,
2065          * so build a frame that has some information in valid format. This is
2066          * really only used for discovery purposes, not to learn exact BSS
2067          * parameters.
2068          */
2069         ies = p2p_build_probe_resp_ies(p2p);
2070         if (ies == NULL)
2071                 return P2P_PREQ_NOT_PROCESSED;
2072
2073         buf = wpabuf_alloc(200 + wpabuf_len(ies));
2074         if (buf == NULL) {
2075                 wpabuf_free(ies);
2076                 return P2P_PREQ_NOT_PROCESSED;
2077         }
2078
2079         resp = NULL;
2080         resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
2081
2082         resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
2083                                            (WLAN_FC_STYPE_PROBE_RESP << 4));
2084         os_memcpy(resp->da, addr, ETH_ALEN);
2085         os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
2086         os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
2087         resp->u.probe_resp.beacon_int = host_to_le16(100);
2088         /* hardware or low-level driver will setup seq_ctrl and timestamp */
2089         resp->u.probe_resp.capab_info =
2090                 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
2091                              WLAN_CAPABILITY_PRIVACY |
2092                              WLAN_CAPABILITY_SHORT_SLOT_TIME);
2093
2094         wpabuf_put_u8(buf, WLAN_EID_SSID);
2095         wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
2096         wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
2097
2098         wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
2099         wpabuf_put_u8(buf, 8);
2100         wpabuf_put_u8(buf, (60 / 5) | 0x80);
2101         wpabuf_put_u8(buf, 90 / 5);
2102         wpabuf_put_u8(buf, (120 / 5) | 0x80);
2103         wpabuf_put_u8(buf, 180 / 5);
2104         wpabuf_put_u8(buf, (240 / 5) | 0x80);
2105         wpabuf_put_u8(buf, 360 / 5);
2106         wpabuf_put_u8(buf, 480 / 5);
2107         wpabuf_put_u8(buf, 540 / 5);
2108
2109         wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
2110         wpabuf_put_u8(buf, 1);
2111         wpabuf_put_u8(buf, p2p->cfg->channel);
2112
2113         wpabuf_put_buf(buf, ies);
2114         wpabuf_free(ies);
2115
2116         p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
2117
2118         wpabuf_free(buf);
2119
2120         return P2P_PREQ_NOT_PROCESSED;
2121 }
2122
2123
2124 enum p2p_probe_req_status
2125 p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
2126                  const u8 *bssid, const u8 *ie, size_t ie_len)
2127 {
2128         enum p2p_probe_req_status res;
2129
2130         p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2131
2132         res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
2133
2134         if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2135             p2p->go_neg_peer &&
2136             os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
2137             == 0) {
2138                 /* Received a Probe Request from GO Negotiation peer */
2139                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2140                         "P2P: Found GO Negotiation peer - try to start GO "
2141                         "negotiation from timeout");
2142                 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
2143                 return P2P_PREQ_PROCESSED;
2144         }
2145
2146         if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2147             p2p->invite_peer &&
2148             os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
2149             == 0) {
2150                 /* Received a Probe Request from Invite peer */
2151                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2152                         "P2P: Found Invite peer - try to start Invite from "
2153                         "timeout");
2154                 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
2155                 return P2P_PREQ_PROCESSED;
2156         }
2157
2158         return res;
2159 }
2160
2161
2162 static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2163                                     u8 *buf, size_t len, struct wpabuf *p2p_ie)
2164 {
2165         struct wpabuf *tmp;
2166         u8 *lpos;
2167         size_t tmplen;
2168         int res;
2169         u8 group_capab;
2170
2171         if (p2p_ie == NULL)
2172                 return 0; /* WLAN AP is not a P2P manager */
2173
2174         /*
2175          * (Re)Association Request - P2P IE
2176          * P2P Capability attribute (shall be present)
2177          * P2P Interface attribute (present if concurrent device and
2178          *      P2P Management is enabled)
2179          */
2180         tmp = wpabuf_alloc(200);
2181         if (tmp == NULL)
2182                 return -1;
2183
2184         lpos = p2p_buf_add_ie_hdr(tmp);
2185         group_capab = 0;
2186         if (p2p->num_groups > 0) {
2187                 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2188                 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2189                     (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2190                     p2p->cross_connect)
2191                         group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2192         }
2193         p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2194         if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2195             (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2196                 p2p_buf_add_p2p_interface(tmp, p2p);
2197         p2p_buf_update_ie_hdr(tmp, lpos);
2198
2199         tmplen = wpabuf_len(tmp);
2200         if (tmplen > len)
2201                 res = -1;
2202         else {
2203                 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2204                 res = tmplen;
2205         }
2206         wpabuf_free(tmp);
2207
2208         return res;
2209 }
2210
2211
2212 int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2213                      size_t len, int p2p_group, struct wpabuf *p2p_ie)
2214 {
2215         struct wpabuf *tmp;
2216         u8 *lpos;
2217         struct p2p_device *peer;
2218         size_t tmplen;
2219         int res;
2220         size_t extra = 0;
2221
2222         if (!p2p_group)
2223                 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2224
2225 #ifdef CONFIG_WIFI_DISPLAY
2226         if (p2p->wfd_ie_assoc_req)
2227                 extra = wpabuf_len(p2p->wfd_ie_assoc_req);
2228 #endif /* CONFIG_WIFI_DISPLAY */
2229
2230         /*
2231          * (Re)Association Request - P2P IE
2232          * P2P Capability attribute (shall be present)
2233          * Extended Listen Timing (may be present)
2234          * P2P Device Info attribute (shall be present)
2235          */
2236         tmp = wpabuf_alloc(200 + extra);
2237         if (tmp == NULL)
2238                 return -1;
2239
2240 #ifdef CONFIG_WIFI_DISPLAY
2241         if (p2p->wfd_ie_assoc_req)
2242                 wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
2243 #endif /* CONFIG_WIFI_DISPLAY */
2244
2245         peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2246
2247         lpos = p2p_buf_add_ie_hdr(tmp);
2248         p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2249         if (p2p->ext_listen_interval)
2250                 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2251                                               p2p->ext_listen_interval);
2252         p2p_buf_add_device_info(tmp, p2p, peer);
2253         p2p_buf_update_ie_hdr(tmp, lpos);
2254
2255         tmplen = wpabuf_len(tmp);
2256         if (tmplen > len)
2257                 res = -1;
2258         else {
2259                 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2260                 res = tmplen;
2261         }
2262         wpabuf_free(tmp);
2263
2264         return res;
2265 }
2266
2267
2268 int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2269 {
2270         struct wpabuf *p2p_ie;
2271         int ret;
2272
2273         p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2274         if (p2p_ie == NULL)
2275                 return 0;
2276
2277         ret = p2p_attr_text(p2p_ie, buf, end);
2278         wpabuf_free(p2p_ie);
2279         return ret;
2280 }
2281
2282
2283 int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
2284 {
2285         struct p2p_message msg;
2286
2287         os_memset(&msg, 0, sizeof(msg));
2288         if (p2p_parse_p2p_ie(p2p_ie, &msg))
2289                 return -1;
2290
2291         if (msg.p2p_device_addr) {
2292                 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2293                 return 0;
2294         } else if (msg.device_id) {
2295                 os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2296                 return 0;
2297         }
2298         return -1;
2299 }
2300
2301
2302 int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2303 {
2304         struct wpabuf *p2p_ie;
2305         int ret;
2306
2307         p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2308                                              P2P_IE_VENDOR_TYPE);
2309         if (p2p_ie == NULL)
2310                 return -1;
2311         ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
2312         wpabuf_free(p2p_ie);
2313         return ret;
2314 }
2315
2316
2317 static void p2p_clear_go_neg(struct p2p_data *p2p)
2318 {
2319         p2p->go_neg_peer = NULL;
2320         p2p_clear_timeout(p2p);
2321         p2p_set_state(p2p, P2P_IDLE);
2322 }
2323
2324
2325 void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2326 {
2327         if (p2p->go_neg_peer == NULL) {
2328                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2329                         "P2P: No pending Group Formation - "
2330                         "ignore WPS registration success notification");
2331                 return; /* No pending Group Formation */
2332         }
2333
2334         if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2335             0) {
2336                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2337                         "P2P: Ignore WPS registration success notification "
2338                         "for " MACSTR " (GO Negotiation peer " MACSTR ")",
2339                         MAC2STR(mac_addr),
2340                         MAC2STR(p2p->go_neg_peer->intended_addr));
2341                 return; /* Ignore unexpected peer address */
2342         }
2343
2344         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2345                 "P2P: Group Formation completed successfully with " MACSTR,
2346                 MAC2STR(mac_addr));
2347
2348         p2p_clear_go_neg(p2p);
2349 }
2350
2351
2352 void p2p_group_formation_failed(struct p2p_data *p2p)
2353 {
2354         if (p2p->go_neg_peer == NULL) {
2355                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2356                         "P2P: No pending Group Formation - "
2357                         "ignore group formation failure notification");
2358                 return; /* No pending Group Formation */
2359         }
2360
2361         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2362                 "P2P: Group Formation failed with " MACSTR,
2363                 MAC2STR(p2p->go_neg_peer->intended_addr));
2364
2365         p2p_clear_go_neg(p2p);
2366 }
2367
2368
2369 struct p2p_data * p2p_init(const struct p2p_config *cfg)
2370 {
2371         struct p2p_data *p2p;
2372
2373         if (cfg->max_peers < 1)
2374                 return NULL;
2375
2376         p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2377         if (p2p == NULL)
2378                 return NULL;
2379         p2p->cfg = (struct p2p_config *) (p2p + 1);
2380         os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2381         if (cfg->dev_name)
2382                 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2383         if (cfg->manufacturer)
2384                 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2385         if (cfg->model_name)
2386                 p2p->cfg->model_name = os_strdup(cfg->model_name);
2387         if (cfg->model_number)
2388                 p2p->cfg->model_number = os_strdup(cfg->model_number);
2389         if (cfg->serial_number)
2390                 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
2391         if (cfg->pref_chan) {
2392                 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
2393                                                 sizeof(struct p2p_channel));
2394                 if (p2p->cfg->pref_chan) {
2395                         os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
2396                                   cfg->num_pref_chan *
2397                                   sizeof(struct p2p_channel));
2398                 } else
2399                         p2p->cfg->num_pref_chan = 0;
2400         }
2401
2402 #ifdef ANDROID_P2P
2403         /* 100ms listen time is too less to receive the response frames in some scenarios
2404          * increasing min listen time to 200ms.
2405          */
2406         p2p->min_disc_int = 2;
2407         /* SD_FAIR_POLICY: Initializing the SD current serviced pointer to NULL */
2408         p2p->sd_dev_list = NULL;
2409 #else
2410         p2p->min_disc_int = 1;
2411 #endif
2412         p2p->max_disc_int = 3;
2413
2414         os_get_random(&p2p->next_tie_breaker, 1);
2415         p2p->next_tie_breaker &= 0x01;
2416         if (cfg->sd_request)
2417                 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2418         p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2419         if (cfg->concurrent_operations)
2420                 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2421         p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2422
2423         dl_list_init(&p2p->devices);
2424
2425         eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
2426                                p2p_expiration_timeout, p2p, NULL);
2427
2428         p2p->go_timeout = 100;
2429         p2p->client_timeout = 20;
2430
2431         return p2p;
2432 }
2433
2434
2435 void p2p_deinit(struct p2p_data *p2p)
2436 {
2437 #ifdef CONFIG_WIFI_DISPLAY
2438         wpabuf_free(p2p->wfd_ie_beacon);
2439         wpabuf_free(p2p->wfd_ie_probe_req);
2440         wpabuf_free(p2p->wfd_ie_probe_resp);
2441         wpabuf_free(p2p->wfd_ie_assoc_req);
2442         wpabuf_free(p2p->wfd_ie_invitation);
2443         wpabuf_free(p2p->wfd_ie_prov_disc_req);
2444         wpabuf_free(p2p->wfd_ie_prov_disc_resp);
2445         wpabuf_free(p2p->wfd_ie_go_neg);
2446         wpabuf_free(p2p->wfd_dev_info);
2447         wpabuf_free(p2p->wfd_assoc_bssid);
2448         wpabuf_free(p2p->wfd_coupled_sink_info);
2449 #endif /* CONFIG_WIFI_DISPLAY */
2450
2451         eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
2452         eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
2453         eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2454         p2p_flush(p2p);
2455         p2p_free_req_dev_types(p2p);
2456         os_free(p2p->cfg->dev_name);
2457         os_free(p2p->cfg->manufacturer);
2458         os_free(p2p->cfg->model_name);
2459         os_free(p2p->cfg->model_number);
2460         os_free(p2p->cfg->serial_number);
2461         os_free(p2p->cfg->pref_chan);
2462         os_free(p2p->groups);
2463         wpabuf_free(p2p->sd_resp);
2464         os_free(p2p->after_scan_tx);
2465         p2p_remove_wps_vendor_extensions(p2p);
2466         os_free(p2p);
2467 }
2468
2469
2470 void p2p_flush(struct p2p_data *p2p)
2471 {
2472         struct p2p_device *dev, *prev;
2473         p2p_stop_find(p2p);
2474         dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
2475                               list) {
2476                 dl_list_del(&dev->list);
2477                 p2p_device_free(p2p, dev);
2478         }
2479 #ifdef ANDROID_P2P
2480         /* SD_FAIR_POLICY: Initializing the SD current serviced pointer to NULL */
2481         p2p->sd_dev_list = NULL;
2482 #endif
2483         p2p_free_sd_queries(p2p);
2484         os_free(p2p->after_scan_tx);
2485         p2p->after_scan_tx = NULL;
2486 }
2487
2488
2489 int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
2490 {
2491         struct p2p_device *dev;
2492
2493         dev = p2p_get_device(p2p, addr);
2494         if (dev == NULL)
2495                 return -1;
2496
2497         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR,
2498                 MAC2STR(addr));
2499
2500         if (p2p->go_neg_peer == dev)
2501                 p2p->go_neg_peer = NULL;
2502
2503         dev->wps_method = WPS_NOT_READY;
2504         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
2505         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
2506
2507         /* Check if after_scan_tx is for this peer. If so free it */
2508         if (p2p->after_scan_tx &&
2509             os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
2510                 os_free(p2p->after_scan_tx);
2511                 p2p->after_scan_tx = NULL;
2512         }
2513
2514         return 0;
2515 }
2516
2517
2518 int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
2519 {
2520         os_free(p2p->cfg->dev_name);
2521         if (dev_name) {
2522                 p2p->cfg->dev_name = os_strdup(dev_name);
2523                 if (p2p->cfg->dev_name == NULL)
2524                         return -1;
2525         } else
2526                 p2p->cfg->dev_name = NULL;
2527         return 0;
2528 }
2529
2530
2531 int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
2532 {
2533         os_free(p2p->cfg->manufacturer);
2534         p2p->cfg->manufacturer = NULL;
2535         if (manufacturer) {
2536                 p2p->cfg->manufacturer = os_strdup(manufacturer);
2537                 if (p2p->cfg->manufacturer == NULL)
2538                         return -1;
2539         }
2540
2541         return 0;
2542 }
2543
2544
2545 int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
2546 {
2547         os_free(p2p->cfg->model_name);
2548         p2p->cfg->model_name = NULL;
2549         if (model_name) {
2550                 p2p->cfg->model_name = os_strdup(model_name);
2551                 if (p2p->cfg->model_name == NULL)
2552                         return -1;
2553         }
2554
2555         return 0;
2556 }
2557
2558
2559 int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
2560 {
2561         os_free(p2p->cfg->model_number);
2562         p2p->cfg->model_number = NULL;
2563         if (model_number) {
2564                 p2p->cfg->model_number = os_strdup(model_number);
2565                 if (p2p->cfg->model_number == NULL)
2566                         return -1;
2567         }
2568
2569         return 0;
2570 }
2571
2572
2573 int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
2574 {
2575         os_free(p2p->cfg->serial_number);
2576         p2p->cfg->serial_number = NULL;
2577         if (serial_number) {
2578                 p2p->cfg->serial_number = os_strdup(serial_number);
2579                 if (p2p->cfg->serial_number == NULL)
2580                         return -1;
2581         }
2582
2583         return 0;
2584 }
2585
2586
2587 void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
2588 {
2589         p2p->cfg->config_methods = config_methods;
2590 }
2591
2592
2593 void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
2594 {
2595         os_memcpy(p2p->cfg->uuid, uuid, 16);
2596 }
2597
2598
2599 int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
2600 {
2601         os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
2602         return 0;
2603 }
2604
2605
2606 int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
2607                           size_t num_dev_types)
2608 {
2609         if (num_dev_types > P2P_SEC_DEVICE_TYPES)
2610                 num_dev_types = P2P_SEC_DEVICE_TYPES;
2611         p2p->cfg->num_sec_dev_types = num_dev_types;
2612         os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
2613         return 0;
2614 }
2615
2616
2617 void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
2618 {
2619         int i;
2620
2621         for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2622                 wpabuf_free(p2p->wps_vendor_ext[i]);
2623                 p2p->wps_vendor_ext[i] = NULL;
2624         }
2625 }
2626
2627
2628 int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
2629                                  const struct wpabuf *vendor_ext)
2630 {
2631         int i;
2632
2633         if (vendor_ext == NULL)
2634                 return -1;
2635
2636         for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2637                 if (p2p->wps_vendor_ext[i] == NULL)
2638                         break;
2639         }
2640         if (i >= P2P_MAX_WPS_VENDOR_EXT)
2641                 return -1;
2642
2643         p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
2644         if (p2p->wps_vendor_ext[i] == NULL)
2645                 return -1;
2646
2647         return 0;
2648 }
2649
2650
2651 int p2p_set_country(struct p2p_data *p2p, const char *country)
2652 {
2653         os_memcpy(p2p->cfg->country, country, 3);
2654         return 0;
2655 }
2656
2657
2658 void p2p_continue_find(struct p2p_data *p2p)
2659 {
2660         struct p2p_device *dev;
2661 #ifdef ANDROID_P2P
2662         int skip=1;
2663 #endif
2664         p2p_set_state(p2p, P2P_SEARCH);
2665         dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2666 #ifdef ANDROID_P2P
2667                 /* SD_FAIR_POLICY: We need to give chance to all devices in the device list
2668                  * There may be a scenario, where a particular peer device have
2669                  * not registered any query response. When we send a SD request to such device,
2670                  * no response will be received. And if we continue to get probe responses from that device, 
2671                  * and if that device happens to be on top in our device list, 
2672                  * we will always continue to send SD requests always to that peer only. 
2673                  * We will not be able to send SD requests to other devices in that case. 
2674                  * This implementation keeps track of last serviced peer device. 
2675                  * And then takes the next one from the device list, in the next iteration.
2676                  */
2677                 if (p2p->sd_dev_list && p2p->sd_dev_list != &p2p->devices) {
2678                         if(skip) {
2679                                 if ((&dev->list == p2p->sd_dev_list) ) {
2680                                         skip = 0;
2681                                         if (dev->list.next == &p2p->devices)
2682                                                 p2p->sd_dev_list = NULL;
2683                                 }
2684                                 continue;
2685                         }
2686                 }
2687                 p2p->sd_dev_list = &dev->list;
2688                 wpa_printf(MSG_DEBUG, "P2P: ### Servicing %p dev->flags 0x%x SD schedule %s devaddr " MACSTR,
2689                         p2p->sd_dev_list, dev->flags, dev->flags & P2P_DEV_SD_SCHEDULE ? "TRUE": "FALSE",
2690                         MAC2STR(dev->info.p2p_device_addr));
2691 #endif
2692                 if (dev->flags & P2P_DEV_SD_SCHEDULE) {
2693                         if (p2p_start_sd(p2p, dev) == 0)
2694                                 return;
2695                         else
2696                                 break;
2697                 } else if (dev->req_config_methods &&
2698                            !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
2699                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2700                                 "pending Provision Discovery Request to "
2701                                 MACSTR " (config methods 0x%x)",
2702                                 MAC2STR(dev->info.p2p_device_addr),
2703                                 dev->req_config_methods);
2704                         if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
2705                                 return;
2706                 }
2707         }
2708
2709         p2p_listen_in_find(p2p);
2710 }
2711
2712
2713 static void p2p_sd_cb(struct p2p_data *p2p, int success)
2714 {
2715         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2716                 "P2P: Service Discovery Query TX callback: success=%d",
2717                 success);
2718         p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2719
2720         if (!success) {
2721                 if (p2p->sd_peer) {
2722                         p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2723                         p2p->sd_peer = NULL;
2724                 }
2725                 p2p_continue_find(p2p);
2726                 return;
2727         }
2728
2729         if (p2p->sd_peer == NULL) {
2730                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2731                         "P2P: No SD peer entry known");
2732                 p2p_continue_find(p2p);
2733                 return;
2734         }
2735
2736         /* Wait for response from the peer */
2737         p2p_set_state(p2p, P2P_SD_DURING_FIND);
2738         p2p_set_timeout(p2p, 0, 200000);
2739 }
2740
2741
2742 /**
2743  * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
2744  * @p2p: P2P module context from p2p_init()
2745  */
2746 static void p2p_retry_pd(struct p2p_data *p2p)
2747 {
2748         struct p2p_device *dev;
2749
2750         if (p2p->state != P2P_IDLE)
2751                 return;
2752
2753         /*
2754          * Retry the prov disc req attempt only for the peer that the user had
2755          * requested for and provided a join has not been initiated on it
2756          * in the meantime.
2757          */
2758
2759         dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2760                 if (os_memcmp(p2p->pending_pd_devaddr,
2761                               dev->info.p2p_device_addr, ETH_ALEN) != 0)
2762                         continue;
2763                 if (!dev->req_config_methods)
2764                         continue;
2765                 if (dev->flags & P2P_DEV_PD_FOR_JOIN)
2766                         continue;
2767
2768                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2769                         "pending Provision Discovery Request to "
2770                         MACSTR " (config methods 0x%x)",
2771                         MAC2STR(dev->info.p2p_device_addr),
2772                         dev->req_config_methods);
2773                 p2p_send_prov_disc_req(p2p, dev, 0, 0);
2774                 return;
2775         }
2776 }
2777
2778
2779 static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
2780 {
2781         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2782                 "P2P: Provision Discovery Request TX callback: success=%d",
2783                 success);
2784
2785         /*
2786          * Postpone resetting the pending action state till after we actually
2787          * time out. This allows us to take some action like notifying any
2788          * interested parties about no response to the request.
2789          *
2790          * When the timer (below) goes off we check in IDLE, SEARCH, or
2791          * LISTEN_ONLY state, which are the only allowed states to issue a PD
2792          * requests in, if this was still pending and then raise notification.
2793          */
2794
2795         if (!success) {
2796                 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2797
2798                 if (p2p->user_initiated_pd &&
2799                     (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
2800                 {
2801                         /* Retry request from timeout to avoid busy loops */
2802                         p2p->pending_action_state = P2P_PENDING_PD;
2803                         p2p_set_timeout(p2p, 0, 50000);
2804                 } else if (p2p->state != P2P_IDLE)
2805                         p2p_continue_find(p2p);
2806                 else if (p2p->user_initiated_pd) {
2807                         p2p->pending_action_state = P2P_PENDING_PD;
2808                         p2p_set_timeout(p2p, 0, 300000);
2809                 }
2810                 return;
2811         }
2812
2813         /*
2814          * This postponing, of resetting pending_action_state, needs to be
2815          * done only for user initiated PD requests and not internal ones.
2816          */
2817         if (p2p->user_initiated_pd)
2818                 p2p->pending_action_state = P2P_PENDING_PD;
2819         else
2820                 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2821
2822         /* Wait for response from the peer */
2823         if (p2p->state == P2P_SEARCH)
2824                 p2p_set_state(p2p, P2P_PD_DURING_FIND);
2825         p2p_set_timeout(p2p, 0, 200000);
2826 }
2827
2828
2829 int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
2830                          int level, const u8 *ies, size_t ies_len)
2831 {
2832         p2p_add_device(p2p, bssid, freq, level, ies, ies_len, 1);
2833
2834         return 0;
2835 }
2836
2837
2838 void p2p_scan_res_handled(struct p2p_data *p2p)
2839 {
2840         if (!p2p->p2p_scan_running) {
2841                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
2842                         "running, but scan results received");
2843         }
2844         p2p->p2p_scan_running = 0;
2845         eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2846
2847         if (p2p_run_after_scan(p2p))
2848                 return;
2849         if (p2p->state == P2P_SEARCH)
2850                 p2p_continue_find(p2p);
2851 }
2852
2853
2854 void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id)
2855 {
2856         u8 *len;
2857
2858 #ifdef CONFIG_WIFI_DISPLAY
2859         if (p2p->wfd_ie_probe_req)
2860                 wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
2861 #endif /* CONFIG_WIFI_DISPLAY */
2862
2863         len = p2p_buf_add_ie_hdr(ies);
2864         p2p_buf_add_capability(ies, p2p->dev_capab &
2865                                ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
2866         if (dev_id)
2867                 p2p_buf_add_device_id(ies, dev_id);
2868         if (p2p->cfg->reg_class && p2p->cfg->channel)
2869                 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
2870                                            p2p->cfg->reg_class,
2871                                            p2p->cfg->channel);
2872         if (p2p->ext_listen_interval)
2873                 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
2874                                               p2p->ext_listen_interval);
2875         /* TODO: p2p_buf_add_operating_channel() if GO */
2876         p2p_buf_update_ie_hdr(ies, len);
2877 }
2878
2879
2880 size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
2881 {
2882         size_t len = 100;
2883
2884 #ifdef CONFIG_WIFI_DISPLAY
2885         if (p2p && p2p->wfd_ie_probe_req)
2886                 len += wpabuf_len(p2p->wfd_ie_probe_req);
2887 #endif /* CONFIG_WIFI_DISPLAY */
2888
2889         return len;
2890 }
2891
2892
2893 int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
2894 {
2895         return p2p_attr_text(p2p_ie, buf, end);
2896 }
2897
2898
2899 static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
2900 {
2901         struct p2p_device *dev = p2p->go_neg_peer;
2902
2903         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2904                 "P2P: GO Negotiation Request TX callback: success=%d",
2905                 success);
2906
2907         if (dev == NULL) {
2908                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2909                         "P2P: No pending GO Negotiation");
2910                 return;
2911         }
2912
2913         if (success) {
2914                 if (dev->flags & P2P_DEV_USER_REJECTED) {
2915                         p2p_set_state(p2p, P2P_IDLE);
2916                         return;
2917                 }
2918         } else if (dev->go_neg_req_sent) {
2919                 /* Cancel the increment from p2p_connect_send() on failure */
2920                 dev->go_neg_req_sent--;
2921         }
2922
2923         if (!success &&
2924             (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
2925             !is_zero_ether_addr(dev->member_in_go_dev)) {
2926                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2927                         "P2P: Peer " MACSTR " did not acknowledge request - "
2928                         "try to use device discoverability through its GO",
2929                         MAC2STR(dev->info.p2p_device_addr));
2930                 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2931                 p2p_send_dev_disc_req(p2p, dev);
2932                 return;
2933         }
2934
2935         /*
2936          * Use P2P find, if needed, to find the other device from its listen
2937          * channel.
2938          */
2939         p2p_set_state(p2p, P2P_CONNECT);
2940         p2p_set_timeout(p2p, 0, success ? 200000 : 100000);
2941 }
2942
2943
2944 static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
2945 {
2946         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2947                 "P2P: GO Negotiation Response TX callback: success=%d",
2948                 success);
2949         if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
2950                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2951                         "P2P: Ignore TX callback event - GO Negotiation is "
2952                         "not running anymore");
2953                 return;
2954         }
2955         p2p_set_state(p2p, P2P_CONNECT);
2956         p2p_set_timeout(p2p, 0, 250000);
2957 }
2958
2959
2960 static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
2961 {
2962         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2963                 "P2P: GO Negotiation Response (failure) TX callback: "
2964                 "success=%d", success);
2965         if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
2966                 p2p_go_neg_failed(p2p, p2p->go_neg_peer,
2967                                   p2p->go_neg_peer->status);
2968         }
2969 }
2970
2971
2972 static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
2973                                enum p2p_send_action_result result)
2974 {
2975         struct p2p_device *dev;
2976
2977         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2978                 "P2P: GO Negotiation Confirm TX callback: result=%d",
2979                 result);
2980         p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2981         if (result == P2P_SEND_ACTION_FAILED) {
2982                 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2983                 return;
2984         }
2985         if (result == P2P_SEND_ACTION_NO_ACK) {
2986                 /*
2987                  * It looks like the TX status for GO Negotiation Confirm is
2988                  * often showing failure even when the peer has actually
2989                  * received the frame. Since the peer may change channels
2990                  * immediately after having received the frame, we may not see
2991                  * an Ack for retries, so just dropping a single frame may
2992                  * trigger this. To allow the group formation to succeed if the
2993                  * peer did indeed receive the frame, continue regardless of
2994                  * the TX status.
2995                  */
2996                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2997                         "P2P: Assume GO Negotiation Confirm TX was actually "
2998                         "received by the peer even though Ack was not "
2999                         "reported");
3000         }
3001
3002         dev = p2p->go_neg_peer;
3003         if (dev == NULL)
3004                 return;
3005
3006         p2p_go_complete(p2p, dev);
3007 }
3008
3009
3010 void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3011                         const u8 *src, const u8 *bssid,
3012                         enum p2p_send_action_result result)
3013 {
3014         enum p2p_pending_action_state state;
3015         int success;
3016
3017         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3018                 "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
3019                 " src=" MACSTR " bssid=" MACSTR " result=%d",
3020                 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
3021                 MAC2STR(bssid), result);
3022         success = result == P2P_SEND_ACTION_SUCCESS;
3023         state = p2p->pending_action_state;
3024         p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3025         switch (state) {
3026         case P2P_NO_PENDING_ACTION:
3027                 break;
3028         case P2P_PENDING_GO_NEG_REQUEST:
3029                 p2p_go_neg_req_cb(p2p, success);
3030                 break;
3031         case P2P_PENDING_GO_NEG_RESPONSE:
3032                 p2p_go_neg_resp_cb(p2p, success);
3033                 break;
3034         case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
3035                 p2p_go_neg_resp_failure_cb(p2p, success);
3036                 break;
3037         case P2P_PENDING_GO_NEG_CONFIRM:
3038                 p2p_go_neg_conf_cb(p2p, result);
3039                 break;
3040         case P2P_PENDING_SD:
3041                 p2p_sd_cb(p2p, success);
3042                 break;
3043         case P2P_PENDING_PD:
3044                 p2p_prov_disc_cb(p2p, success);
3045                 break;
3046         case P2P_PENDING_INVITATION_REQUEST:
3047                 p2p_invitation_req_cb(p2p, success);
3048                 break;
3049         case P2P_PENDING_INVITATION_RESPONSE:
3050                 p2p_invitation_resp_cb(p2p, success);
3051                 break;
3052         case P2P_PENDING_DEV_DISC_REQUEST:
3053                 p2p_dev_disc_req_cb(p2p, success);
3054                 break;
3055         case P2P_PENDING_DEV_DISC_RESPONSE:
3056                 p2p_dev_disc_resp_cb(p2p, success);
3057                 break;
3058         case P2P_PENDING_GO_DISC_REQ:
3059                 p2p_go_disc_req_cb(p2p, success);
3060                 break;
3061         }
3062 }
3063
3064
3065 void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
3066                    unsigned int duration)
3067 {
3068         if (freq == p2p->pending_client_disc_freq) {
3069                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3070                         "P2P: Client discoverability remain-awake completed");
3071                 p2p->pending_client_disc_freq = 0;
3072                 return;
3073         }
3074
3075         if (freq != p2p->pending_listen_freq) {
3076                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3077                         "P2P: Unexpected listen callback for freq=%u "
3078                         "duration=%u (pending_listen_freq=%u)",
3079                         freq, duration, p2p->pending_listen_freq);
3080                 return;
3081         }
3082
3083         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3084                 "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
3085                 "callback",
3086                 p2p->pending_listen_sec, p2p->pending_listen_usec,
3087                 p2p->pending_listen_freq);
3088         p2p->in_listen = 1;
3089         p2p->drv_in_listen = freq;
3090         if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
3091                 /*
3092                  * Add 20 msec extra wait to avoid race condition with driver
3093                  * remain-on-channel end event, i.e., give driver more time to
3094                  * complete the operation before our timeout expires.
3095                  */
3096                 p2p_set_timeout(p2p, p2p->pending_listen_sec,
3097                                 p2p->pending_listen_usec + 20000);
3098         }
3099
3100         p2p->pending_listen_freq = 0;
3101 }
3102
3103
3104 int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
3105 {
3106         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
3107                 "state (freq=%u)", freq);
3108         p2p->drv_in_listen = 0;
3109         if (p2p->in_listen)
3110                 return 0; /* Internal timeout will trigger the next step */
3111
3112         if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
3113                 if (p2p->go_neg_peer->connect_reqs >= 120) {
3114                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3115                                 "P2P: Timeout on sending GO Negotiation "
3116                                 "Request without getting response");
3117                         p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3118                         return 0;
3119                 }
3120
3121                 p2p_set_state(p2p, P2P_CONNECT);
3122                 p2p_connect_send(p2p, p2p->go_neg_peer);
3123                 return 1;
3124         } else if (p2p->state == P2P_SEARCH) {
3125                 if (p2p->p2p_scan_running) {
3126                          /*
3127                           * Search is already in progress. This can happen if
3128                           * an Action frame RX is reported immediately after
3129                           * the end of a remain-on-channel operation and the
3130                           * response frame to that is sent using an offchannel
3131                           * operation while in p2p_find. Avoid an attempt to
3132                           * restart a scan here.
3133                           */
3134                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan "
3135                                 "already in progress - do not try to start a "
3136                                 "new one");
3137                         return 1;
3138                 }
3139                 if (p2p->pending_listen_freq) {
3140                         /*
3141                          * Better wait a bit if the driver is unable to start
3142                          * offchannel operation for some reason. p2p_search()
3143                          * will be started from internal timeout.
3144                          */
3145                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Listen "
3146                                 "operation did not seem to start - delay "
3147                                 "search phase to avoid busy loop");
3148                         p2p_set_timeout(p2p, 0, 100000);
3149                         return 1;
3150                 }
3151                 if (p2p->search_delay) {
3152                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
3153                                 "search operation by %u ms",
3154                                 p2p->search_delay);
3155                         p2p_set_timeout(p2p, p2p->search_delay / 1000,
3156                                         (p2p->search_delay % 1000) * 1000);
3157                         return 1;
3158                 }
3159                 p2p_search(p2p);
3160                 return 1;
3161         }
3162
3163         return 0;
3164 }
3165
3166
3167 static void p2p_timeout_connect(struct p2p_data *p2p)
3168 {
3169         p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3170         if (p2p->go_neg_peer &&
3171             (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
3172                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Wait for GO "
3173                         "Negotiation Confirm timed out - assume GO "
3174                         "Negotiation failed");
3175                 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3176                 return;
3177         }
3178         p2p_set_state(p2p, P2P_CONNECT_LISTEN);
3179         p2p_listen_in_find(p2p);
3180 }
3181
3182
3183 static void p2p_timeout_connect_listen(struct p2p_data *p2p)
3184 {
3185         if (p2p->go_neg_peer) {
3186                 if (p2p->drv_in_listen) {
3187                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
3188                                 "still in Listen state; wait for it to "
3189                                 "complete");
3190                         return;
3191                 }
3192
3193                 if (p2p->go_neg_peer->connect_reqs >= 120) {
3194                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3195                                 "P2P: Timeout on sending GO Negotiation "
3196                                 "Request without getting response");
3197                         p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3198                         return;
3199                 }
3200
3201                 p2p_set_state(p2p, P2P_CONNECT);
3202                 p2p_connect_send(p2p, p2p->go_neg_peer);
3203         } else
3204                 p2p_set_state(p2p, P2P_IDLE);
3205 }
3206
3207
3208 static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
3209 {
3210         /*
3211          * TODO: could remain constantly in Listen state for some time if there
3212          * are no other concurrent uses for the radio. For now, go to listen
3213          * state once per second to give other uses a chance to use the radio.
3214          */
3215         p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
3216         p2p_set_timeout(p2p, 0, 500000);
3217 }
3218
3219
3220 static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
3221 {
3222         struct p2p_device *dev = p2p->go_neg_peer;
3223
3224         if (dev == NULL) {
3225                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3226                         "P2P: Unknown GO Neg peer - stop GO Neg wait");
3227                 return;
3228         }
3229
3230         dev->wait_count++;
3231         if (dev->wait_count >= 120) {
3232                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3233                         "P2P: Timeout on waiting peer to become ready for GO "
3234                         "Negotiation");
3235                 p2p_go_neg_failed(p2p, dev, -1);
3236                 return;
3237         }
3238
3239         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3240                 "P2P: Go to Listen state while waiting for the peer to become "
3241                 "ready for GO Negotiation");
3242         p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
3243         p2p_listen_in_find(p2p);
3244 }
3245
3246
3247 static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
3248 {
3249         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3250                 "P2P: Service Discovery Query timeout");
3251         if (p2p->sd_peer) {
3252                 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3253                 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
3254                 p2p->sd_peer = NULL;
3255         }
3256         p2p_continue_find(p2p);
3257 }
3258
3259
3260 static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
3261 {
3262         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3263                 "P2P: Provision Discovery Request timeout");
3264         p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3265         p2p_continue_find(p2p);
3266 }
3267
3268
3269 static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
3270 {
3271         p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3272
3273         /*
3274          * For user initiated PD requests that we have not gotten any responses
3275          * for while in IDLE state, we retry them a couple of times before
3276          * giving up.
3277          */
3278         if (!p2p->user_initiated_pd)
3279                 return;
3280
3281         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3282                 "P2P: User initiated Provision Discovery Request timeout");
3283
3284         if (p2p->pd_retries) {
3285                 p2p->pd_retries--;
3286                 p2p_retry_pd(p2p);
3287         } else {
3288                 if (p2p->cfg->prov_disc_fail)
3289                         p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
3290                                                  p2p->pending_pd_devaddr,
3291                                                  P2P_PROV_DISC_TIMEOUT);
3292                 p2p_reset_pending_pd(p2p);
3293         }
3294 }
3295
3296
3297 static void p2p_timeout_invite(struct p2p_data *p2p)
3298 {
3299         p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3300         p2p_set_state(p2p, P2P_INVITE_LISTEN);
3301         if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
3302                 /*
3303                  * Better remain on operating channel instead of listen channel
3304                  * when running a group.
3305                  */
3306                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
3307                         "active GO role - wait on operating channel");
3308                 p2p_set_timeout(p2p, 0, 100000);
3309                 return;
3310         }
3311         p2p_listen_in_find(p2p);
3312 }
3313
3314
3315 static void p2p_timeout_invite_listen(struct p2p_data *p2p)
3316 {
3317         if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
3318                 p2p_set_state(p2p, P2P_INVITE);
3319                 p2p_invite_send(p2p, p2p->invite_peer,
3320                                 p2p->invite_go_dev_addr);
3321         } else {
3322                 if (p2p->invite_peer) {
3323                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3324                                 "P2P: Invitation Request retry limit reached");
3325                         if (p2p->cfg->invitation_result)
3326                                 p2p->cfg->invitation_result(
3327                                         p2p->cfg->cb_ctx, -1, NULL);
3328                 }
3329                 p2p_set_state(p2p, P2P_IDLE);
3330         }
3331 }
3332
3333
3334 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
3335 {
3336         struct p2p_data *p2p = eloop_ctx;
3337
3338         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
3339                 p2p_state_txt(p2p->state));
3340
3341         p2p->in_listen = 0;
3342
3343         switch (p2p->state) {
3344         case P2P_IDLE:
3345                 /* Check if we timed out waiting for PD req */
3346                 if (p2p->pending_action_state == P2P_PENDING_PD)
3347                         p2p_timeout_prov_disc_req(p2p);
3348                 break;
3349         case P2P_SEARCH:
3350                 /* Check if we timed out waiting for PD req */
3351                 if (p2p->pending_action_state == P2P_PENDING_PD)
3352                         p2p_timeout_prov_disc_req(p2p);
3353                 if (p2p->search_delay && !p2p->in_search_delay) {
3354                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
3355                                 "search operation by %u ms",
3356                                 p2p->search_delay);
3357                         p2p->in_search_delay = 1;
3358                         p2p_set_timeout(p2p, p2p->search_delay / 1000,
3359                                         (p2p->search_delay % 1000) * 1000);
3360                         break;
3361                 }
3362                 p2p->in_search_delay = 0;
3363                 p2p_search(p2p);
3364                 break;
3365         case P2P_CONNECT:
3366                 p2p_timeout_connect(p2p);
3367                 break;
3368         case P2P_CONNECT_LISTEN:
3369                 p2p_timeout_connect_listen(p2p);
3370                 break;
3371         case P2P_GO_NEG:
3372                 break;
3373         case P2P_LISTEN_ONLY:
3374                 /* Check if we timed out waiting for PD req */
3375                 if (p2p->pending_action_state == P2P_PENDING_PD)
3376                         p2p_timeout_prov_disc_req(p2p);
3377
3378                 if (p2p->ext_listen_only) {
3379                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3380                                 "P2P: Extended Listen Timing - Listen State "
3381                                 "completed");
3382                         p2p->ext_listen_only = 0;
3383                         p2p_set_state(p2p, P2P_IDLE);
3384                 }
3385                 break;
3386         case P2P_WAIT_PEER_CONNECT:
3387                 p2p_timeout_wait_peer_connect(p2p);
3388                 break;
3389         case P2P_WAIT_PEER_IDLE:
3390                 p2p_timeout_wait_peer_idle(p2p);
3391                 break;
3392         case P2P_SD_DURING_FIND:
3393                 p2p_timeout_sd_during_find(p2p);
3394                 break;
3395         case P2P_PROVISIONING:
3396                 break;
3397         case P2P_PD_DURING_FIND:
3398                 p2p_timeout_prov_disc_during_find(p2p);
3399                 break;
3400         case P2P_INVITE:
3401                 p2p_timeout_invite(p2p);
3402                 break;
3403         case P2P_INVITE_LISTEN:
3404                 p2p_timeout_invite_listen(p2p);
3405                 break;
3406         case P2P_SEARCH_WHEN_READY:
3407                 break;
3408         case P2P_CONTINUE_SEARCH_WHEN_READY:
3409                 break;
3410         }
3411 }
3412
3413
3414 int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
3415 {
3416         struct p2p_device *dev;
3417
3418         dev = p2p_get_device(p2p, peer_addr);
3419         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
3420                 "connection attempts by peer " MACSTR, MAC2STR(peer_addr));
3421         if (dev == NULL) {
3422                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
3423                         " unknown", MAC2STR(peer_addr));
3424                 return -1;
3425         }
3426         dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
3427         dev->flags |= P2P_DEV_USER_REJECTED;
3428         return 0;
3429 }
3430
3431
3432 const char * p2p_wps_method_text(enum p2p_wps_method method)
3433 {
3434         switch (method) {
3435         case WPS_NOT_READY:
3436                 return "not-ready";
3437         case WPS_PIN_DISPLAY:
3438                 return "Display";
3439         case WPS_PIN_KEYPAD:
3440                 return "Keypad";
3441         case WPS_PBC:
3442                 return "PBC";
3443         }
3444
3445         return "??";
3446 }
3447
3448
3449 static const char * p2p_go_state_text(enum p2p_go_state go_state)
3450 {
3451         switch (go_state) {
3452         case UNKNOWN_GO:
3453                 return "unknown";
3454         case LOCAL_GO:
3455                 return "local";
3456         case  REMOTE_GO:
3457                 return "remote";
3458         }
3459
3460         return "??";
3461 }
3462
3463
3464 const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
3465                                                const u8 *addr, int next)
3466 {
3467         struct p2p_device *dev;
3468
3469         if (addr)
3470                 dev = p2p_get_device(p2p, addr);
3471         else
3472                 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3473
3474         if (dev && next) {
3475                 dev = dl_list_first(&dev->list, struct p2p_device, list);
3476                 if (&dev->list == &p2p->devices)
3477                         dev = NULL;
3478         }
3479
3480         if (dev == NULL)
3481                 return NULL;
3482
3483         return &dev->info;
3484 }
3485
3486
3487 int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
3488                           char *buf, size_t buflen)
3489 {
3490         struct p2p_device *dev;
3491         int res;
3492         char *pos, *end;
3493         struct os_time now;
3494
3495         if (info == NULL)
3496                 return -1;
3497
3498         dev = (struct p2p_device *) (((u8 *) info) -
3499                                      offsetof(struct p2p_device, info));
3500
3501         pos = buf;
3502         end = buf + buflen;
3503
3504         os_get_time(&now);
3505         res = os_snprintf(pos, end - pos,
3506                           "age=%d\n"
3507                           "listen_freq=%d\n"
3508                           "wps_method=%s\n"
3509                           "interface_addr=" MACSTR "\n"
3510                           "member_in_go_dev=" MACSTR "\n"
3511                           "member_in_go_iface=" MACSTR "\n"
3512                           "go_neg_req_sent=%d\n"
3513                           "go_state=%s\n"
3514                           "dialog_token=%u\n"
3515                           "intended_addr=" MACSTR "\n"
3516                           "country=%c%c\n"
3517                           "oper_freq=%d\n"
3518                           "req_config_methods=0x%x\n"
3519                           "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
3520                           "status=%d\n"
3521                           "wait_count=%u\n"
3522                           "invitation_reqs=%u\n",
3523                           (int) (now.sec - dev->last_seen.sec),
3524                           dev->listen_freq,
3525                           p2p_wps_method_text(dev->wps_method),
3526                           MAC2STR(dev->interface_addr),
3527                           MAC2STR(dev->member_in_go_dev),
3528                           MAC2STR(dev->member_in_go_iface),
3529                           dev->go_neg_req_sent,
3530                           p2p_go_state_text(dev->go_state),
3531                           dev->dialog_token,
3532                           MAC2STR(dev->intended_addr),
3533                           dev->country[0] ? dev->country[0] : '_',
3534                           dev->country[1] ? dev->country[1] : '_',
3535                           dev->oper_freq,
3536                           dev->req_config_methods,
3537                           dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
3538                           "[PROBE_REQ_ONLY]" : "",
3539                           dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
3540                           dev->flags & P2P_DEV_NOT_YET_READY ?
3541                           "[NOT_YET_READY]" : "",
3542                           dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
3543                           dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
3544                           "",
3545                           dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
3546                           "[PD_PEER_DISPLAY]" : "",
3547                           dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
3548                           "[PD_PEER_KEYPAD]" : "",
3549                           dev->flags & P2P_DEV_USER_REJECTED ?
3550                           "[USER_REJECTED]" : "",
3551                           dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
3552                           "[PEER_WAITING_RESPONSE]" : "",
3553                           dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
3554                           "[PREFER_PERSISTENT_GROUP]" : "",
3555                           dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
3556                           "[WAIT_GO_NEG_RESPONSE]" : "",
3557                           dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
3558                           "[WAIT_GO_NEG_CONFIRM]" : "",
3559                           dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
3560                           "[GROUP_CLIENT_ONLY]" : "",
3561                           dev->flags & P2P_DEV_FORCE_FREQ ?
3562                           "[FORCE_FREQ]" : "",
3563                           dev->flags & P2P_DEV_PD_FOR_JOIN ?
3564                           "[PD_FOR_JOIN]" : "",
3565                           dev->status,
3566                           dev->wait_count,
3567                           dev->invitation_reqs);
3568         if (res < 0 || res >= end - pos)
3569                 return pos - buf;
3570         pos += res;
3571
3572         if (dev->ext_listen_period) {
3573                 res = os_snprintf(pos, end - pos,
3574                                   "ext_listen_period=%u\n"
3575                                   "ext_listen_interval=%u\n",
3576                                   dev->ext_listen_period,
3577                                   dev->ext_listen_interval);
3578                 if (res < 0 || res >= end - pos)
3579                         return pos - buf;
3580                 pos += res;
3581         }
3582
3583         if (dev->oper_ssid_len) {
3584                 res = os_snprintf(pos, end - pos,
3585                                   "oper_ssid=%s\n",
3586                                   wpa_ssid_txt(dev->oper_ssid,
3587                                                dev->oper_ssid_len));
3588                 if (res < 0 || res >= end - pos)
3589                         return pos - buf;
3590                 pos += res;
3591         }
3592
3593 #ifdef CONFIG_WIFI_DISPLAY
3594         if (dev->info.wfd_subelems) {
3595                 res = os_snprintf(pos, end - pos, "wfd_subelems=");
3596                 if (res < 0 || res >= end - pos)
3597                         return pos - buf;
3598                 pos += res;
3599
3600                 pos += wpa_snprintf_hex(pos, end - pos,
3601                                         wpabuf_head(dev->info.wfd_subelems),
3602                                         wpabuf_len(dev->info.wfd_subelems));
3603
3604                 res = os_snprintf(pos, end - pos, "\n");
3605                 if (res < 0 || res >= end - pos)
3606                         return pos - buf;
3607                 pos += res;
3608         }
3609 #endif /* CONFIG_WIFI_DISPLAY */
3610
3611         return pos - buf;
3612 }
3613
3614
3615 int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
3616 {
3617         return p2p_get_device(p2p, addr) != NULL;
3618 }
3619
3620
3621 void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
3622 {
3623         if (enabled) {
3624                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3625                         "discoverability enabled");
3626                 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3627         } else {
3628                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3629                         "discoverability disabled");
3630                 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3631         }
3632 }
3633
3634
3635 static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
3636                                               u32 duration2, u32 interval2)
3637 {
3638         struct wpabuf *req;
3639         struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
3640         u8 *len;
3641
3642         req = wpabuf_alloc(100);
3643         if (req == NULL)
3644                 return NULL;
3645
3646         if (duration1 || interval1) {
3647                 os_memset(&desc1, 0, sizeof(desc1));
3648                 desc1.count_type = 1;
3649                 desc1.duration = duration1;
3650                 desc1.interval = interval1;
3651                 ptr1 = &desc1;
3652
3653                 if (duration2 || interval2) {
3654                         os_memset(&desc2, 0, sizeof(desc2));
3655                         desc2.count_type = 2;
3656                         desc2.duration = duration2;
3657                         desc2.interval = interval2;
3658                         ptr2 = &desc2;
3659                 }
3660         }
3661
3662         p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
3663         len = p2p_buf_add_ie_hdr(req);
3664         p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
3665         p2p_buf_update_ie_hdr(req, len);
3666
3667         return req;
3668 }
3669
3670
3671 int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
3672                      const u8 *own_interface_addr, unsigned int freq,
3673                      u32 duration1, u32 interval1, u32 duration2,
3674                      u32 interval2)
3675 {
3676         struct wpabuf *req;
3677
3678         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
3679                 "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
3680                 "int1=%u dur2=%u int2=%u",
3681                 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
3682                 freq, duration1, interval1, duration2, interval2);
3683
3684         req = p2p_build_presence_req(duration1, interval1, duration2,
3685                                      interval2);
3686         if (req == NULL)
3687                 return -1;
3688
3689         p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3690         if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
3691                             go_interface_addr,
3692                             wpabuf_head(req), wpabuf_len(req), 200) < 0) {
3693                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3694                         "P2P: Failed to send Action frame");
3695         }
3696         wpabuf_free(req);
3697
3698         return 0;
3699 }
3700
3701
3702 static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
3703                                                size_t noa_len, u8 dialog_token)
3704 {
3705         struct wpabuf *resp;
3706         u8 *len;
3707
3708         resp = wpabuf_alloc(100 + noa_len);
3709         if (resp == NULL)
3710                 return NULL;
3711
3712         p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
3713         len = p2p_buf_add_ie_hdr(resp);
3714         p2p_buf_add_status(resp, status);
3715         if (noa) {
3716                 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
3717                 wpabuf_put_le16(resp, noa_len);
3718                 wpabuf_put_data(resp, noa, noa_len);
3719         } else
3720                 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
3721         p2p_buf_update_ie_hdr(resp, len);
3722
3723         return resp;
3724 }
3725
3726
3727 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
3728                                      const u8 *sa, const u8 *data, size_t len,
3729                                      int rx_freq)
3730 {
3731         struct p2p_message msg;
3732         u8 status;
3733         struct wpabuf *resp;
3734         size_t g;
3735         struct p2p_group *group = NULL;
3736         int parsed = 0;
3737         u8 noa[50];
3738         int noa_len;
3739
3740         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3741                 "P2P: Received P2P Action - P2P Presence Request");
3742
3743         for (g = 0; g < p2p->num_groups; g++) {
3744                 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
3745                               ETH_ALEN) == 0) {
3746                         group = p2p->groups[g];
3747                         break;
3748                 }
3749         }
3750         if (group == NULL) {
3751                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3752                         "P2P: Ignore P2P Presence Request for unknown group "
3753                         MACSTR, MAC2STR(da));
3754                 return;
3755         }
3756
3757         if (p2p_parse(data, len, &msg) < 0) {
3758                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3759                         "P2P: Failed to parse P2P Presence Request");
3760                 status = P2P_SC_FAIL_INVALID_PARAMS;
3761                 goto fail;
3762         }
3763         parsed = 1;
3764
3765         if (msg.noa == NULL) {
3766                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3767                         "P2P: No NoA attribute in P2P Presence Request");
3768                 status = P2P_SC_FAIL_INVALID_PARAMS;
3769                 goto fail;
3770         }
3771
3772         status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
3773
3774 fail:
3775         if (p2p->cfg->get_noa)
3776                 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
3777                                             sizeof(noa));
3778         else
3779                 noa_len = -1;
3780         resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
3781                                        noa_len > 0 ? noa_len : 0,
3782                                        msg.dialog_token);
3783         if (parsed)
3784                 p2p_parse_free(&msg);
3785         if (resp == NULL)
3786                 return;
3787
3788         p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3789         if (p2p_send_action(p2p, rx_freq, sa, da, da,
3790                             wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
3791                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3792                         "P2P: Failed to send Action frame");
3793         }
3794         wpabuf_free(resp);
3795 }
3796
3797
3798 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
3799                                       const u8 *sa, const u8 *data, size_t len)
3800 {
3801         struct p2p_message msg;
3802
3803         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3804                 "P2P: Received P2P Action - P2P Presence Response");
3805
3806         if (p2p_parse(data, len, &msg) < 0) {
3807                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3808                         "P2P: Failed to parse P2P Presence Response");
3809                 return;
3810         }
3811
3812         if (msg.status == NULL || msg.noa == NULL) {
3813                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3814                         "P2P: No Status or NoA attribute in P2P Presence "
3815                         "Response");
3816                 p2p_parse_free(&msg);
3817                 return;
3818         }
3819
3820         if (*msg.status) {
3821                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3822                         "P2P: P2P Presence Request was rejected: status %u",
3823                         *msg.status);
3824                 p2p_parse_free(&msg);
3825                 return;
3826         }
3827
3828         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3829                 "P2P: P2P Presence Request was accepted");
3830         wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
3831                     msg.noa, msg.noa_len);
3832         /* TODO: process NoA */
3833         p2p_parse_free(&msg);
3834 }
3835
3836
3837 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3838 {
3839         struct p2p_data *p2p = eloop_ctx;
3840
3841         if (p2p->ext_listen_interval) {
3842                 /* Schedule next extended listen timeout */
3843                 eloop_register_timeout(p2p->ext_listen_interval_sec,
3844                                        p2p->ext_listen_interval_usec,
3845                                        p2p_ext_listen_timeout, p2p, NULL);
3846         }
3847
3848         if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
3849                 /*
3850                  * This should not really happen, but it looks like the Listen
3851                  * command may fail is something else (e.g., a scan) was
3852                  * running at an inconvenient time. As a workaround, allow new
3853                  * Extended Listen operation to be started.
3854                  */
3855                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
3856                         "Extended Listen operation had not been completed - "
3857                         "try again");
3858                 p2p->ext_listen_only = 0;
3859                 p2p_set_state(p2p, P2P_IDLE);
3860         }
3861
3862         if (p2p->state != P2P_IDLE) {
3863                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
3864                         "Listen timeout in active state (%s)",
3865                         p2p_state_txt(p2p->state));
3866                 return;
3867         }
3868
3869         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
3870         p2p->ext_listen_only = 1;
3871         if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
3872                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
3873                         "Listen state for Extended Listen Timing");
3874                 p2p->ext_listen_only = 0;
3875         }
3876 }
3877
3878
3879 int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
3880                    unsigned int interval)
3881 {
3882         if (period > 65535 || interval > 65535 || period > interval ||
3883             (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
3884                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3885                         "P2P: Invalid Extended Listen Timing request: "
3886                         "period=%u interval=%u", period, interval);
3887                 return -1;
3888         }
3889
3890         eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
3891
3892         if (interval == 0) {
3893                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3894                         "P2P: Disabling Extended Listen Timing");
3895                 p2p->ext_listen_period = 0;
3896                 p2p->ext_listen_interval = 0;
3897                 return 0;
3898         }
3899
3900         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3901                 "P2P: Enabling Extended Listen Timing: period %u msec, "
3902                 "interval %u msec", period, interval);
3903         p2p->ext_listen_period = period;
3904         p2p->ext_listen_interval = interval;
3905         p2p->ext_listen_interval_sec = interval / 1000;
3906         p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
3907
3908         eloop_register_timeout(p2p->ext_listen_interval_sec,
3909                                p2p->ext_listen_interval_usec,
3910                                p2p_ext_listen_timeout, p2p, NULL);
3911
3912         return 0;
3913 }
3914
3915
3916 void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3917                       const u8 *ie, size_t ie_len)
3918 {
3919         struct p2p_message msg;
3920
3921         if (bssid == NULL || ie == NULL)
3922                 return;
3923
3924         os_memset(&msg, 0, sizeof(msg));
3925         if (p2p_parse_ies(ie, ie_len, &msg))
3926                 return;
3927         if (msg.minor_reason_code == NULL)
3928                 return;
3929
3930         wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3931                 "P2P: Deauthentication notification BSSID " MACSTR
3932                 " reason_code=%u minor_reason_code=%u",
3933                 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3934
3935         p2p_parse_free(&msg);
3936 }
3937
3938
3939 void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3940                         const u8 *ie, size_t ie_len)
3941 {
3942         struct p2p_message msg;
3943
3944         if (bssid == NULL || ie == NULL)
3945                 return;
3946
3947         os_memset(&msg, 0, sizeof(msg));
3948         if (p2p_parse_ies(ie, ie_len, &msg))
3949                 return;
3950         if (msg.minor_reason_code == NULL)
3951                 return;
3952
3953         wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3954                 "P2P: Disassociation notification BSSID " MACSTR
3955                 " reason_code=%u minor_reason_code=%u",
3956                 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3957
3958         p2p_parse_free(&msg);
3959 }
3960
3961
3962 void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
3963 {
3964         if (enabled) {
3965                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3966                         "Device operations enabled");
3967                 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
3968         } else {
3969                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3970                         "Device operations disabled");
3971                 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
3972         }
3973 }
3974
3975
3976 int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
3977 {
3978         if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
3979                 return -1;
3980
3981         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
3982                 "reg_class %u channel %u", reg_class, channel);
3983         p2p->cfg->reg_class = reg_class;
3984         p2p->cfg->channel = channel;
3985
3986         return 0;
3987 }
3988
3989
3990 int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
3991 {
3992         wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
3993         if (postfix == NULL) {
3994                 p2p->cfg->ssid_postfix_len = 0;
3995                 return 0;
3996         }
3997         if (len > sizeof(p2p->cfg->ssid_postfix))
3998                 return -1;
3999         os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
4000         p2p->cfg->ssid_postfix_len = len;
4001         return 0;
4002 }
4003
4004
4005 int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
4006                          int cfg_op_channel)
4007 {
4008         if (p2p_channel_to_freq(p2p->cfg->country, op_reg_class, op_channel)
4009             < 0)
4010                 return -1;
4011
4012         wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, "P2P: Set Operating channel: "
4013                 "reg_class %u channel %u", op_reg_class, op_channel);
4014         p2p->cfg->op_reg_class = op_reg_class;
4015         p2p->cfg->op_channel = op_channel;
4016         p2p->cfg->cfg_op_channel = cfg_op_channel;
4017         return 0;
4018 }
4019
4020
4021 int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
4022                       const struct p2p_channel *pref_chan)
4023 {
4024         struct p2p_channel *n;
4025
4026         if (pref_chan) {
4027                 n = os_malloc(num_pref_chan * sizeof(struct p2p_channel));
4028                 if (n == NULL)
4029                         return -1;
4030                 os_memcpy(n, pref_chan,
4031                           num_pref_chan * sizeof(struct p2p_channel));
4032         } else
4033                 n = NULL;
4034
4035         os_free(p2p->cfg->pref_chan);
4036         p2p->cfg->pref_chan = n;
4037         p2p->cfg->num_pref_chan = num_pref_chan;
4038
4039         return 0;
4040 }
4041
4042
4043 int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
4044                            u8 *iface_addr)
4045 {
4046         struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
4047         if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
4048                 return -1;
4049         os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
4050         return 0;
4051 }
4052
4053
4054 int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
4055                            u8 *dev_addr)
4056 {
4057         struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4058         if (dev == NULL)
4059                 return -1;
4060         os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
4061         return 0;
4062 }
4063
4064
4065 void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
4066 {
4067         os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
4068         if (is_zero_ether_addr(p2p->peer_filter))
4069                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
4070                         "filter");
4071         else
4072                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
4073                         "filter for " MACSTR, MAC2STR(p2p->peer_filter));
4074 }
4075
4076
4077 void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
4078 {
4079         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
4080                 enabled ? "enabled" : "disabled");
4081         if (p2p->cross_connect == enabled)
4082                 return;
4083         p2p->cross_connect = enabled;
4084         /* TODO: may need to tear down any action group where we are GO(?) */
4085 }
4086
4087
4088 int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
4089 {
4090         struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4091         if (dev == NULL)
4092                 return -1;
4093         if (dev->oper_freq <= 0)
4094                 return -1;
4095         return dev->oper_freq;
4096 }
4097
4098
4099 void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
4100 {
4101         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s",
4102                 enabled ? "enabled" : "disabled");
4103         p2p->cfg->p2p_intra_bss = enabled;
4104 }
4105
4106
4107 void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan)
4108 {
4109         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list");
4110         os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
4111 }
4112
4113
4114 int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
4115                     const u8 *src, const u8 *bssid, const u8 *buf,
4116                     size_t len, unsigned int wait_time)
4117 {
4118         if (p2p->p2p_scan_running) {
4119                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action "
4120                         "frame TX until p2p_scan completes");
4121                 if (p2p->after_scan_tx) {
4122                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
4123                                 "previous pending Action frame TX");
4124                         os_free(p2p->after_scan_tx);
4125                 }
4126                 p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
4127                                                len);
4128                 if (p2p->after_scan_tx == NULL)
4129                         return -1;
4130                 p2p->after_scan_tx->freq = freq;
4131                 os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
4132                 os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
4133                 os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
4134                 p2p->after_scan_tx->len = len;
4135                 p2p->after_scan_tx->wait_time = wait_time;
4136                 os_memcpy(p2p->after_scan_tx + 1, buf, len);
4137                 return 0;
4138         }
4139
4140         return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
4141                                      buf, len, wait_time);
4142 }
4143
4144
4145 void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
4146                            int freq_overall)
4147 {
4148         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d,"
4149                 "  5 GHz: %d,  overall: %d", freq_24, freq_5, freq_overall);
4150         p2p->best_freq_24 = freq_24;
4151         p2p->best_freq_5 = freq_5;
4152         p2p->best_freq_overall = freq_overall;
4153 }
4154
4155
4156 const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
4157 {
4158         if (p2p == NULL || p2p->go_neg_peer == NULL)
4159                 return NULL;
4160         return p2p->go_neg_peer->info.p2p_device_addr;
4161 }
4162
4163
4164 const struct p2p_peer_info *
4165 p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
4166 {
4167         struct p2p_device *dev;
4168
4169         if (addr) {
4170                 dev = p2p_get_device(p2p, addr);
4171                 if (!dev)
4172                         return NULL;
4173
4174                 if (!next) {
4175                         if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
4176                                 return NULL;
4177
4178                         return &dev->info;
4179                 } else {
4180                         do {
4181                                 dev = dl_list_first(&dev->list,
4182                                                     struct p2p_device,
4183                                                     list);
4184                                 if (&dev->list == &p2p->devices)
4185                                         return NULL;
4186                         } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
4187                 }
4188         } else {
4189                 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4190                 if (!dev)
4191                         return NULL;
4192                 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
4193                         dev = dl_list_first(&dev->list,
4194                                             struct p2p_device,
4195                                             list);
4196                         if (&dev->list == &p2p->devices)
4197                                 return NULL;
4198                 }
4199         }
4200
4201         return &dev->info;
4202 }
4203
4204 #ifdef ANDROID_P2P
4205 int p2p_search_in_progress(struct p2p_data *p2p)
4206 {
4207         if (p2p == NULL)
4208                 return 0;
4209
4210         return p2p->state == P2P_SEARCH;
4211 }
4212 #endif
4213
4214 int p2p_in_progress(struct p2p_data *p2p)
4215 {
4216         if (p2p == NULL)
4217                 return 0;
4218         if (p2p->state == P2P_SEARCH || p2p->state == P2P_SEARCH_WHEN_READY ||
4219             p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY)
4220                 return 2;
4221         return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
4222 }
4223
4224
4225 void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
4226                             u8 client_timeout)
4227 {
4228         if (p2p) {
4229                 p2p->go_timeout = go_timeout;
4230                 p2p->client_timeout = client_timeout;
4231         }
4232 }
4233
4234
4235 void p2p_increase_search_delay(struct p2p_data *p2p, unsigned int delay)
4236 {
4237         if (p2p && p2p->search_delay < delay)
4238                 p2p->search_delay = delay;
4239 }
4240
4241
4242 #ifdef CONFIG_WIFI_DISPLAY
4243
4244 static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
4245 {
4246         size_t g;
4247         struct p2p_group *group;
4248
4249         for (g = 0; g < p2p->num_groups; g++) {
4250                 group = p2p->groups[g];
4251                 p2p_group_update_ies(group);
4252         }
4253 }
4254
4255
4256 int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
4257 {
4258         wpabuf_free(p2p->wfd_ie_beacon);
4259         p2p->wfd_ie_beacon = ie;
4260         p2p_update_wfd_ie_groups(p2p);
4261         return 0;
4262 }
4263
4264
4265 int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
4266 {
4267         wpabuf_free(p2p->wfd_ie_probe_req);
4268         p2p->wfd_ie_probe_req = ie;
4269         return 0;
4270 }
4271
4272
4273 int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
4274 {
4275         wpabuf_free(p2p->wfd_ie_probe_resp);
4276         p2p->wfd_ie_probe_resp = ie;
4277         p2p_update_wfd_ie_groups(p2p);
4278         return 0;
4279 }
4280
4281
4282 int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
4283 {
4284         wpabuf_free(p2p->wfd_ie_assoc_req);
4285         p2p->wfd_ie_assoc_req = ie;
4286         return 0;
4287 }
4288
4289
4290 int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
4291 {
4292         wpabuf_free(p2p->wfd_ie_invitation);
4293         p2p->wfd_ie_invitation = ie;
4294         return 0;
4295 }
4296
4297
4298 int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
4299 {
4300         wpabuf_free(p2p->wfd_ie_prov_disc_req);
4301         p2p->wfd_ie_prov_disc_req = ie;
4302         return 0;
4303 }
4304
4305
4306 int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
4307 {
4308         wpabuf_free(p2p->wfd_ie_prov_disc_resp);
4309         p2p->wfd_ie_prov_disc_resp = ie;
4310         return 0;
4311 }
4312
4313
4314 int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
4315 {
4316         wpabuf_free(p2p->wfd_ie_go_neg);
4317         p2p->wfd_ie_go_neg = ie;
4318         return 0;
4319 }
4320
4321
4322 int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
4323 {
4324         wpabuf_free(p2p->wfd_dev_info);
4325         if (elem) {
4326                 p2p->wfd_dev_info = wpabuf_dup(elem);
4327                 if (p2p->wfd_dev_info == NULL)
4328                         return -1;
4329         } else
4330                 p2p->wfd_dev_info = NULL;
4331
4332         return 0;
4333 }
4334
4335
4336 int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
4337 {
4338         wpabuf_free(p2p->wfd_assoc_bssid);
4339         if (elem) {
4340                 p2p->wfd_assoc_bssid = wpabuf_dup(elem);
4341                 if (p2p->wfd_assoc_bssid == NULL)
4342                         return -1;
4343         } else
4344                 p2p->wfd_assoc_bssid = NULL;
4345
4346         return 0;
4347 }
4348
4349
4350 int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
4351                                   const struct wpabuf *elem)
4352 {
4353         wpabuf_free(p2p->wfd_coupled_sink_info);
4354         if (elem) {
4355                 p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
4356                 if (p2p->wfd_coupled_sink_info == NULL)
4357                         return -1;
4358         } else
4359                 p2p->wfd_coupled_sink_info = NULL;
4360
4361         return 0;
4362 }
4363
4364 #endif /* CONFIG_WIFI_DISPLAY */