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