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