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