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