OSDN Git Service

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