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