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