P2P: Allow all channels with multi-channel concurrency
[mech_eap.git] / src / p2p / p2p.c
1 /*
2  * Wi-Fi Direct - P2P module
3  * Copyright (c) 2009-2010, Atheros Communications
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/ieee802_11_common.h"
15 #include "common/wpa_ctrl.h"
16 #include "wps/wps_i.h"
17 #include "p2p_i.h"
18 #include "p2p.h"
19
20
21 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx);
22 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev);
23 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
24                                      const u8 *sa, const u8 *data, size_t len,
25                                      int rx_freq);
26 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
27                                       const u8 *sa, const u8 *data,
28                                       size_t len);
29 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx);
30 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
31
32
33 /*
34  * p2p_scan recovery timeout
35  *
36  * Many drivers are using 30 second timeout on scan results. Allow a bit larger
37  * timeout for this to avoid hitting P2P timeout unnecessarily.
38  */
39 #define P2P_SCAN_TIMEOUT 35
40
41 /**
42  * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
43  * entries will be removed
44  */
45 #define P2P_PEER_EXPIRATION_AGE 300
46
47 #define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2)
48
49 static void p2p_expire_peers(struct p2p_data *p2p)
50 {
51         struct p2p_device *dev, *n;
52         struct os_time now;
53         size_t i;
54
55         os_get_time(&now);
56         dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
57                 if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
58                         continue;
59
60                 if (p2p->cfg->go_connected &&
61                     p2p->cfg->go_connected(p2p->cfg->cb_ctx,
62                                            dev->info.p2p_device_addr)) {
63                         /*
64                          * We are connected as a client to a group in which the
65                          * peer is the GO, so do not expire the peer entry.
66                          */
67                         os_get_time(&dev->last_seen);
68                         continue;
69                 }
70
71                 for (i = 0; i < p2p->num_groups; i++) {
72                         if (p2p_group_is_client_connected(
73                                     p2p->groups[i], dev->info.p2p_device_addr))
74                                 break;
75                 }
76                 if (i < p2p->num_groups) {
77                         /*
78                          * The peer is connected as a client in a group where
79                          * we are the GO, so do not expire the peer entry.
80                          */
81                         os_get_time(&dev->last_seen);
82                         continue;
83                 }
84
85                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Expiring old peer "
86                         "entry " MACSTR, MAC2STR(dev->info.p2p_device_addr));
87                 dl_list_del(&dev->list);
88                 p2p_device_free(p2p, dev);
89         }
90 }
91
92
93 static void p2p_expiration_timeout(void *eloop_ctx, void *timeout_ctx)
94 {
95         struct p2p_data *p2p = eloop_ctx;
96         p2p_expire_peers(p2p);
97         eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
98                                p2p_expiration_timeout, p2p, NULL);
99 }
100
101
102 static const char * p2p_state_txt(int state)
103 {
104         switch (state) {
105         case P2P_IDLE:
106                 return "IDLE";
107         case P2P_SEARCH:
108                 return "SEARCH";
109         case P2P_CONNECT:
110                 return "CONNECT";
111         case P2P_CONNECT_LISTEN:
112                 return "CONNECT_LISTEN";
113         case P2P_GO_NEG:
114                 return "GO_NEG";
115         case P2P_LISTEN_ONLY:
116                 return "LISTEN_ONLY";
117         case P2P_WAIT_PEER_CONNECT:
118                 return "WAIT_PEER_CONNECT";
119         case P2P_WAIT_PEER_IDLE:
120                 return "WAIT_PEER_IDLE";
121         case P2P_SD_DURING_FIND:
122                 return "SD_DURING_FIND";
123         case P2P_PROVISIONING:
124                 return "PROVISIONING";
125         case P2P_PD_DURING_FIND:
126                 return "PD_DURING_FIND";
127         case P2P_INVITE:
128                 return "INVITE";
129         case P2P_INVITE_LISTEN:
130                 return "INVITE_LISTEN";
131         case P2P_SEARCH_WHEN_READY:
132                 return "SEARCH_WHEN_READY";
133         case P2P_CONTINUE_SEARCH_WHEN_READY:
134                 return "CONTINUE_SEARCH_WHEN_READY";
135         default:
136                 return "?";
137         }
138 }
139
140
141 u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
142 {
143         struct p2p_device *dev = NULL;
144
145         if (!addr || !p2p)
146                 return 0;
147
148         dev = p2p_get_device(p2p, addr);
149         if (dev)
150                 return dev->wps_prov_info;
151         else
152                 return 0;
153 }
154
155
156 void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr)
157 {
158         struct p2p_device *dev = NULL;
159
160         if (!addr || !p2p)
161                 return;
162
163         dev = p2p_get_device(p2p, addr);
164         if (dev)
165                 dev->wps_prov_info = 0;
166 }
167
168
169 void p2p_set_state(struct p2p_data *p2p, int new_state)
170 {
171         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: State %s -> %s",
172                 p2p_state_txt(p2p->state), p2p_state_txt(new_state));
173         p2p->state = new_state;
174 }
175
176
177 void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
178 {
179         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
180                 "P2P: Set timeout (state=%s): %u.%06u sec",
181                 p2p_state_txt(p2p->state), sec, usec);
182         eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
183         eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
184 }
185
186
187 void p2p_clear_timeout(struct p2p_data *p2p)
188 {
189         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear timeout (state=%s)",
190                 p2p_state_txt(p2p->state));
191         eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
192 }
193
194
195 void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer,
196                        int status)
197 {
198         struct p2p_go_neg_results res;
199         p2p_clear_timeout(p2p);
200         p2p_set_state(p2p, P2P_IDLE);
201         if (p2p->go_neg_peer)
202                 p2p->go_neg_peer->wps_method = WPS_NOT_READY;
203         p2p->go_neg_peer = NULL;
204
205         os_memset(&res, 0, sizeof(res));
206         res.status = status;
207         if (peer) {
208                 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr,
209                           ETH_ALEN);
210                 os_memcpy(res.peer_interface_addr, peer->intended_addr,
211                           ETH_ALEN);
212         }
213         p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
214 }
215
216
217 static void p2p_listen_in_find(struct p2p_data *p2p)
218 {
219         unsigned int r, tu;
220         int freq;
221         struct wpabuf *ies;
222
223         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
224                 "P2P: Starting short listen state (state=%s)",
225                 p2p_state_txt(p2p->state));
226
227         freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
228                                    p2p->cfg->channel);
229         if (freq < 0) {
230                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
231                         "P2P: Unknown regulatory class/channel");
232                 return;
233         }
234
235         os_get_random((u8 *) &r, sizeof(r));
236         tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
237               p2p->min_disc_int) * 100;
238
239         p2p->pending_listen_freq = freq;
240         p2p->pending_listen_sec = 0;
241         p2p->pending_listen_usec = 1024 * tu;
242
243         ies = p2p_build_probe_resp_ies(p2p);
244         if (ies == NULL)
245                 return;
246
247         if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
248                     ies) < 0) {
249                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
250                         "P2P: Failed to start listen mode");
251                 p2p->pending_listen_freq = 0;
252         }
253         wpabuf_free(ies);
254 }
255
256
257 int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
258 {
259         int freq;
260         struct wpabuf *ies;
261
262         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
263                 "P2P: Going to listen(only) state");
264
265         freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
266                                    p2p->cfg->channel);
267         if (freq < 0) {
268                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
269                         "P2P: Unknown regulatory class/channel");
270                 return -1;
271         }
272
273         p2p->pending_listen_freq = freq;
274         p2p->pending_listen_sec = timeout / 1000;
275         p2p->pending_listen_usec = (timeout % 1000) * 1000;
276
277         if (p2p->p2p_scan_running) {
278                 if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
279                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
280                                 "P2P: p2p_scan running - connect is already "
281                                 "pending - skip listen");
282                         return 0;
283                 }
284                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
285                         "P2P: p2p_scan running - delay start of listen state");
286                 p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
287                 return 0;
288         }
289
290         ies = p2p_build_probe_resp_ies(p2p);
291         if (ies == NULL)
292                 return -1;
293
294         if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
295                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
296                         "P2P: Failed to start listen mode");
297                 p2p->pending_listen_freq = 0;
298                 wpabuf_free(ies);
299                 return -1;
300         }
301         wpabuf_free(ies);
302
303         p2p_set_state(p2p, P2P_LISTEN_ONLY);
304
305         return 0;
306 }
307
308
309 static void p2p_device_clear_reported(struct p2p_data *p2p)
310 {
311         struct p2p_device *dev;
312         dl_list_for_each(dev, &p2p->devices, struct p2p_device, list)
313                 dev->flags &= ~P2P_DEV_REPORTED;
314 }
315
316
317 /**
318  * p2p_get_device - Fetch a peer entry
319  * @p2p: P2P module context from p2p_init()
320  * @addr: P2P Device Address of the peer
321  * Returns: Pointer to the device entry or %NULL if not found
322  */
323 struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
324 {
325         struct p2p_device *dev;
326         dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
327                 if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0)
328                         return dev;
329         }
330         return NULL;
331 }
332
333
334 /**
335  * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
336  * @p2p: P2P module context from p2p_init()
337  * @addr: P2P Interface Address of the peer
338  * Returns: Pointer to the device entry or %NULL if not found
339  */
340 struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
341                                              const u8 *addr)
342 {
343         struct p2p_device *dev;
344         dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
345                 if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
346                         return dev;
347         }
348         return NULL;
349 }
350
351
352 /**
353  * p2p_create_device - Create a peer entry
354  * @p2p: P2P module context from p2p_init()
355  * @addr: P2P Device Address of the peer
356  * Returns: Pointer to the device entry or %NULL on failure
357  *
358  * If there is already an entry for the peer, it will be returned instead of
359  * creating a new one.
360  */
361 static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
362                                              const u8 *addr)
363 {
364         struct p2p_device *dev, *oldest = NULL;
365         size_t count = 0;
366
367         dev = p2p_get_device(p2p, addr);
368         if (dev)
369                 return dev;
370
371         dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
372                 count++;
373                 if (oldest == NULL ||
374                     os_time_before(&dev->last_seen, &oldest->last_seen))
375                         oldest = dev;
376         }
377         if (count + 1 > p2p->cfg->max_peers && oldest) {
378                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
379                         "P2P: Remove oldest peer entry to make room for a new "
380                         "peer");
381                 dl_list_del(&oldest->list);
382                 p2p_device_free(p2p, oldest);
383         }
384
385         dev = os_zalloc(sizeof(*dev));
386         if (dev == NULL)
387                 return NULL;
388         dl_list_add(&p2p->devices, &dev->list);
389         os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
390
391         return dev;
392 }
393
394
395 static void p2p_copy_client_info(struct p2p_device *dev,
396                                  struct p2p_client_info *cli)
397 {
398         os_memcpy(dev->info.device_name, cli->dev_name, cli->dev_name_len);
399         dev->info.device_name[cli->dev_name_len] = '\0';
400         dev->info.dev_capab = cli->dev_capab;
401         dev->info.config_methods = cli->config_methods;
402         os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
403         dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
404         os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
405                   dev->info.wps_sec_dev_type_list_len);
406 }
407
408
409 static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
410                                  const u8 *go_interface_addr, int freq,
411                                  const u8 *gi, size_t gi_len)
412 {
413         struct p2p_group_info info;
414         size_t c;
415         struct p2p_device *dev;
416
417         if (gi == NULL)
418                 return 0;
419
420         if (p2p_group_info_parse(gi, gi_len, &info) < 0)
421                 return -1;
422
423         /*
424          * Clear old data for this group; if the devices are still in the
425          * group, the information will be restored in the loop following this.
426          */
427         dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
428                 if (os_memcmp(dev->member_in_go_iface, go_interface_addr,
429                               ETH_ALEN) == 0) {
430                         os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
431                         os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
432                 }
433         }
434
435         for (c = 0; c < info.num_clients; c++) {
436                 struct p2p_client_info *cli = &info.client[c];
437                 if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr,
438                               ETH_ALEN) == 0)
439                         continue; /* ignore our own entry */
440                 dev = p2p_get_device(p2p, cli->p2p_device_addr);
441                 if (dev) {
442                         if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
443                                           P2P_DEV_PROBE_REQ_ONLY)) {
444                                 /*
445                                  * Update information since we have not
446                                  * received this directly from the client.
447                                  */
448                                 p2p_copy_client_info(dev, cli);
449                         } else {
450                                 /*
451                                  * Need to update P2P Client Discoverability
452                                  * flag since it is valid only in P2P Group
453                                  * Info attribute.
454                                  */
455                                 dev->info.dev_capab &=
456                                         ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
457                                 dev->info.dev_capab |=
458                                         cli->dev_capab &
459                                         P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
460                         }
461                         if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
462                                 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
463                         }
464                 } else {
465                         dev = p2p_create_device(p2p, cli->p2p_device_addr);
466                         if (dev == NULL)
467                                 continue;
468                         dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
469                         p2p_copy_client_info(dev, cli);
470                         dev->oper_freq = freq;
471                         p2p->cfg->dev_found(p2p->cfg->cb_ctx,
472                                             dev->info.p2p_device_addr,
473                                             &dev->info, 1);
474                         dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
475                 }
476
477                 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
478                           ETH_ALEN);
479                 os_get_time(&dev->last_seen);
480                 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
481                 os_memcpy(dev->member_in_go_iface, go_interface_addr,
482                           ETH_ALEN);
483         }
484
485         return 0;
486 }
487
488
489 static void p2p_copy_wps_info(struct p2p_device *dev, int probe_req,
490                               const struct p2p_message *msg)
491 {
492         os_memcpy(dev->info.device_name, msg->device_name,
493                   sizeof(dev->info.device_name));
494
495         if (msg->manufacturer &&
496             msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
497                 os_memset(dev->info.manufacturer, 0,
498                           sizeof(dev->info.manufacturer));
499                 os_memcpy(dev->info.manufacturer, msg->manufacturer,
500                           msg->manufacturer_len);
501         }
502
503         if (msg->model_name &&
504             msg->model_name_len < sizeof(dev->info.model_name)) {
505                 os_memset(dev->info.model_name, 0,
506                           sizeof(dev->info.model_name));
507                 os_memcpy(dev->info.model_name, msg->model_name,
508                           msg->model_name_len);
509         }
510
511         if (msg->model_number &&
512             msg->model_number_len < sizeof(dev->info.model_number)) {
513                 os_memset(dev->info.model_number, 0,
514                           sizeof(dev->info.model_number));
515                 os_memcpy(dev->info.model_number, msg->model_number,
516                           msg->model_number_len);
517         }
518
519         if (msg->serial_number &&
520             msg->serial_number_len < sizeof(dev->info.serial_number)) {
521                 os_memset(dev->info.serial_number, 0,
522                           sizeof(dev->info.serial_number));
523                 os_memcpy(dev->info.serial_number, msg->serial_number,
524                           msg->serial_number_len);
525         }
526
527         if (msg->pri_dev_type)
528                 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
529                           sizeof(dev->info.pri_dev_type));
530         else if (msg->wps_pri_dev_type)
531                 os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
532                           sizeof(dev->info.pri_dev_type));
533
534         if (msg->wps_sec_dev_type_list) {
535                 os_memcpy(dev->info.wps_sec_dev_type_list,
536                           msg->wps_sec_dev_type_list,
537                           msg->wps_sec_dev_type_list_len);
538                 dev->info.wps_sec_dev_type_list_len =
539                         msg->wps_sec_dev_type_list_len;
540         }
541
542         if (msg->capability) {
543                 /*
544                  * P2P Client Discoverability bit is reserved in all frames
545                  * that use this function, so do not change its value here.
546                  */
547                 dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
548                 dev->info.dev_capab |= msg->capability[0] &
549                         ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
550                 dev->info.group_capab = msg->capability[1];
551         }
552
553         if (msg->ext_listen_timing) {
554                 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
555                 dev->ext_listen_interval =
556                         WPA_GET_LE16(msg->ext_listen_timing + 2);
557         }
558
559         if (!probe_req) {
560                 dev->info.config_methods = msg->config_methods ?
561                         msg->config_methods : msg->wps_config_methods;
562         }
563 }
564
565
566 /**
567  * p2p_add_device - Add peer entries based on scan results or P2P frames
568  * @p2p: P2P module context from p2p_init()
569  * @addr: Source address of Beacon or Probe Response frame (may be either
570  *      P2P Device Address or P2P Interface Address)
571  * @level: Signal level (signal strength of the received frame from the peer)
572  * @freq: Frequency on which the Beacon or Probe Response frame was received
573  * @ies: IEs from the Beacon or Probe Response frame
574  * @ies_len: Length of ies buffer in octets
575  * @scan_res: Whether this was based on scan results
576  * Returns: 0 on success, -1 on failure
577  *
578  * If the scan result is for a GO, the clients in the group will also be added
579  * to the peer table. This function can also be used with some other frames
580  * like Provision Discovery Request that contains P2P Capability and P2P Device
581  * Info attributes.
582  */
583 int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level,
584                    const u8 *ies, size_t ies_len, int scan_res)
585 {
586         struct p2p_device *dev;
587         struct p2p_message msg;
588         const u8 *p2p_dev_addr;
589         int i;
590
591         os_memset(&msg, 0, sizeof(msg));
592         if (p2p_parse_ies(ies, ies_len, &msg)) {
593                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
594                         "P2P: Failed to parse P2P IE for a device entry");
595                 p2p_parse_free(&msg);
596                 return -1;
597         }
598
599         if (msg.p2p_device_addr)
600                 p2p_dev_addr = msg.p2p_device_addr;
601         else if (msg.device_id)
602                 p2p_dev_addr = msg.device_id;
603         else {
604                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
605                         "P2P: Ignore scan data without P2P Device Info or "
606                         "P2P Device Id");
607                 p2p_parse_free(&msg);
608                 return -1;
609         }
610
611         if (!is_zero_ether_addr(p2p->peer_filter) &&
612             os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
613                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer "
614                         "filter for " MACSTR " due to peer filter",
615                         MAC2STR(p2p_dev_addr));
616                 return 0;
617         }
618
619         dev = p2p_create_device(p2p, p2p_dev_addr);
620         if (dev == NULL) {
621                 p2p_parse_free(&msg);
622                 return -1;
623         }
624         os_get_time(&dev->last_seen);
625         dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
626
627         if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
628                 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
629         if (msg.ssid &&
630             (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
631              os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
632              != 0)) {
633                 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
634                 dev->oper_ssid_len = msg.ssid[1];
635         }
636
637         if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
638             *msg.ds_params >= 1 && *msg.ds_params <= 14) {
639                 int ds_freq;
640                 if (*msg.ds_params == 14)
641                         ds_freq = 2484;
642                 else
643                         ds_freq = 2407 + *msg.ds_params * 5;
644                 if (freq != ds_freq) {
645                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
646                                 "P2P: Update Listen frequency based on DS "
647                                 "Parameter Set IE: %d -> %d MHz",
648                                 freq, ds_freq);
649                         freq = ds_freq;
650                 }
651         }
652
653         if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
654                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
655                         "P2P: Update Listen frequency based on scan "
656                         "results (" MACSTR " %d -> %d MHz (DS param %d)",
657                         MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
658                         freq, msg.ds_params ? *msg.ds_params : -1);
659         }
660         if (scan_res) {
661                 dev->listen_freq = freq;
662                 if (msg.group_info)
663                         dev->oper_freq = freq;
664         }
665         dev->info.level = level;
666
667         p2p_copy_wps_info(dev, 0, &msg);
668
669         for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
670                 wpabuf_free(dev->info.wps_vendor_ext[i]);
671                 dev->info.wps_vendor_ext[i] = NULL;
672         }
673
674         for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
675                 if (msg.wps_vendor_ext[i] == NULL)
676                         break;
677                 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
678                         msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
679                 if (dev->info.wps_vendor_ext[i] == NULL)
680                         break;
681         }
682
683         if (msg.wfd_subelems) {
684                 wpabuf_free(dev->info.wfd_subelems);
685                 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
686         }
687
688         if (scan_res) {
689                 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
690                                       msg.group_info, msg.group_info_len);
691         }
692
693         p2p_parse_free(&msg);
694
695         if (p2p_pending_sd_req(p2p, dev))
696                 dev->flags |= P2P_DEV_SD_SCHEDULE;
697
698         if (dev->flags & P2P_DEV_REPORTED)
699                 return 0;
700
701         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
702                 "P2P: Peer found with Listen frequency %d MHz", freq);
703         if (dev->flags & P2P_DEV_USER_REJECTED) {
704                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
705                         "P2P: Do not report rejected device");
706                 return 0;
707         }
708
709         p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
710                             !(dev->flags & P2P_DEV_REPORTED_ONCE));
711         dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
712
713         return 0;
714 }
715
716
717 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
718 {
719         int i;
720
721         if (p2p->go_neg_peer == dev) {
722                 /*
723                  * If GO Negotiation is in progress, report that it has failed.
724                  */
725                 p2p_go_neg_failed(p2p, dev, -1);
726                 p2p->go_neg_peer = NULL;
727         }
728         if (p2p->invite_peer == dev)
729                 p2p->invite_peer = NULL;
730         if (p2p->sd_peer == dev)
731                 p2p->sd_peer = NULL;
732         if (p2p->pending_client_disc_go == dev)
733                 p2p->pending_client_disc_go = NULL;
734
735         /* dev_lost() device, but only if it was previously dev_found() */
736         if (dev->flags & P2P_DEV_REPORTED_ONCE)
737                 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
738                                    dev->info.p2p_device_addr);
739
740         for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
741                 wpabuf_free(dev->info.wps_vendor_ext[i]);
742                 dev->info.wps_vendor_ext[i] = NULL;
743         }
744
745         wpabuf_free(dev->info.wfd_subelems);
746
747         os_free(dev);
748 }
749
750
751 static int p2p_get_next_prog_freq(struct p2p_data *p2p)
752 {
753         struct p2p_channels *c;
754         struct p2p_reg_class *cla;
755         size_t cl, ch;
756         int found = 0;
757         u8 reg_class;
758         u8 channel;
759         int freq;
760
761         c = &p2p->cfg->channels;
762         for (cl = 0; cl < c->reg_classes; cl++) {
763                 cla = &c->reg_class[cl];
764                 if (cla->reg_class != p2p->last_prog_scan_class)
765                         continue;
766                 for (ch = 0; ch < cla->channels; ch++) {
767                         if (cla->channel[ch] == p2p->last_prog_scan_chan) {
768                                 found = 1;
769                                 break;
770                         }
771                 }
772                 if (found)
773                         break;
774         }
775
776         if (!found) {
777                 /* Start from beginning */
778                 reg_class = c->reg_class[0].reg_class;
779                 channel = c->reg_class[0].channel[0];
780         } else {
781                 /* Pick the next channel */
782                 ch++;
783                 if (ch == cla->channels) {
784                         cl++;
785                         if (cl == c->reg_classes)
786                                 cl = 0;
787                         ch = 0;
788                 }
789                 reg_class = c->reg_class[cl].reg_class;
790                 channel = c->reg_class[cl].channel[ch];
791         }
792
793         freq = p2p_channel_to_freq(p2p->cfg->country, reg_class, channel);
794         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search "
795                 "channel: reg_class %u channel %u -> %d MHz",
796                 reg_class, channel, freq);
797         p2p->last_prog_scan_class = reg_class;
798         p2p->last_prog_scan_chan = channel;
799
800         if (freq == 2412 || freq == 2437 || freq == 2462)
801                 return 0; /* No need to add social channels */
802         return freq;
803 }
804
805
806 static void p2p_search(struct p2p_data *p2p)
807 {
808         int freq = 0;
809         enum p2p_scan_type type;
810         u16 pw_id = DEV_PW_DEFAULT;
811         int res;
812
813         if (p2p->drv_in_listen) {
814                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still "
815                         "in Listen state - wait for it to end before "
816                         "continuing");
817                 return;
818         }
819         p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
820
821         if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
822             (freq = p2p_get_next_prog_freq(p2p)) > 0) {
823                 type = P2P_SCAN_SOCIAL_PLUS_ONE;
824                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
825                         "(+ freq %u)", freq);
826         } else {
827                 type = P2P_SCAN_SOCIAL;
828                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search");
829         }
830
831         res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
832                                  p2p->num_req_dev_types, p2p->req_dev_types,
833                                  p2p->find_dev_id, pw_id);
834         if (res < 0) {
835                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
836                         "P2P: Scan request failed");
837                 p2p_continue_find(p2p);
838         } else if (res == 1) {
839                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
840                         "p2p_scan at this point - will try again after "
841                         "previous scan completes");
842                 p2p_set_state(p2p, P2P_CONTINUE_SEARCH_WHEN_READY);
843         } else {
844                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
845                 p2p->p2p_scan_running = 1;
846                 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
847                 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
848                                        p2p, NULL);
849         }
850 }
851
852
853 static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
854 {
855         struct p2p_data *p2p = eloop_ctx;
856         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop");
857         p2p_stop_find(p2p);
858 }
859
860
861 static int p2p_run_after_scan(struct p2p_data *p2p)
862 {
863         struct p2p_device *dev;
864         enum p2p_after_scan op;
865
866         if (p2p->after_scan_tx) {
867                 /* TODO: schedule p2p_run_after_scan to be called from TX
868                  * status callback(?) */
869                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending "
870                         "Action frame at p2p_scan completion");
871                 p2p->cfg->send_action(p2p->cfg->cb_ctx,
872                                       p2p->after_scan_tx->freq,
873                                       p2p->after_scan_tx->dst,
874                                       p2p->after_scan_tx->src,
875                                       p2p->after_scan_tx->bssid,
876                                       (u8 *) (p2p->after_scan_tx + 1),
877                                       p2p->after_scan_tx->len,
878                                       p2p->after_scan_tx->wait_time);
879                 os_free(p2p->after_scan_tx);
880                 p2p->after_scan_tx = NULL;
881                 return 1;
882         }
883
884         op = p2p->start_after_scan;
885         p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
886         switch (op) {
887         case P2P_AFTER_SCAN_NOTHING:
888                 break;
889         case P2P_AFTER_SCAN_LISTEN:
890                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
891                         "requested Listen state");
892                 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
893                            p2p->pending_listen_usec / 1000);
894                 return 1;
895         case P2P_AFTER_SCAN_CONNECT:
896                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
897                         "requested connect with " MACSTR,
898                         MAC2STR(p2p->after_scan_peer));
899                 dev = p2p_get_device(p2p, p2p->after_scan_peer);
900                 if (dev == NULL) {
901                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
902                                 "known anymore");
903                         break;
904                 }
905                 p2p_connect_send(p2p, dev);
906                 return 1;
907         }
908
909         return 0;
910 }
911
912
913 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
914 {
915         struct p2p_data *p2p = eloop_ctx;
916         int running;
917         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout "
918                 "(running=%d)", p2p->p2p_scan_running);
919         running = p2p->p2p_scan_running;
920         /* Make sure we recover from missed scan results callback */
921         p2p->p2p_scan_running = 0;
922
923         if (running)
924                 p2p_run_after_scan(p2p);
925 }
926
927
928 static void p2p_free_req_dev_types(struct p2p_data *p2p)
929 {
930         p2p->num_req_dev_types = 0;
931         os_free(p2p->req_dev_types);
932         p2p->req_dev_types = NULL;
933 }
934
935
936 int p2p_find(struct p2p_data *p2p, unsigned int timeout,
937              enum p2p_discovery_type type,
938              unsigned int num_req_dev_types, const u8 *req_dev_types,
939              const u8 *dev_id, unsigned int search_delay)
940 {
941         int res;
942
943         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
944                 type);
945         if (p2p->p2p_scan_running) {
946                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
947                         "already running");
948         }
949
950         p2p_free_req_dev_types(p2p);
951         if (req_dev_types && num_req_dev_types) {
952                 p2p->req_dev_types = os_malloc(num_req_dev_types *
953                                                WPS_DEV_TYPE_LEN);
954                 if (p2p->req_dev_types == NULL)
955                         return -1;
956                 os_memcpy(p2p->req_dev_types, req_dev_types,
957                           num_req_dev_types * WPS_DEV_TYPE_LEN);
958                 p2p->num_req_dev_types = num_req_dev_types;
959         }
960
961         if (dev_id) {
962                 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
963                 p2p->find_dev_id = p2p->find_dev_id_buf;
964         } else
965                 p2p->find_dev_id = NULL;
966
967         p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
968         p2p_clear_timeout(p2p);
969         p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
970         p2p->find_type = type;
971         p2p_device_clear_reported(p2p);
972         p2p_set_state(p2p, P2P_SEARCH);
973         p2p->search_delay = search_delay;
974         p2p->in_search_delay = 0;
975         eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
976         p2p->last_p2p_find_timeout = timeout;
977         if (timeout)
978                 eloop_register_timeout(timeout, 0, p2p_find_timeout,
979                                        p2p, NULL);
980         switch (type) {
981         case P2P_FIND_START_WITH_FULL:
982         case P2P_FIND_PROGRESSIVE:
983                 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
984                                          p2p->num_req_dev_types,
985                                          p2p->req_dev_types, dev_id,
986                                          DEV_PW_DEFAULT);
987                 break;
988         case P2P_FIND_ONLY_SOCIAL:
989                 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
990                                          p2p->num_req_dev_types,
991                                          p2p->req_dev_types, dev_id,
992                                          DEV_PW_DEFAULT);
993                 break;
994         default:
995                 return -1;
996         }
997
998         if (res == 0) {
999                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
1000                 p2p->p2p_scan_running = 1;
1001                 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1002                 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
1003                                        p2p, NULL);
1004         } else if (res == 1) {
1005                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
1006                         "p2p_scan at this point - will try again after "
1007                         "previous scan completes");
1008                 res = 0;
1009                 p2p_set_state(p2p, P2P_SEARCH_WHEN_READY);
1010                 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1011         } else {
1012                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
1013                         "p2p_scan");
1014                 p2p_set_state(p2p, P2P_IDLE);
1015                 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1016         }
1017
1018         return res;
1019 }
1020
1021
1022 int p2p_other_scan_completed(struct p2p_data *p2p)
1023 {
1024         if (p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY) {
1025                 p2p_set_state(p2p, P2P_SEARCH);
1026                 p2p_search(p2p);
1027                 return 1;
1028         }
1029         if (p2p->state != P2P_SEARCH_WHEN_READY)
1030                 return 0;
1031         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting pending P2P find "
1032                 "now that previous scan was completed");
1033         if (p2p_find(p2p, p2p->last_p2p_find_timeout, p2p->find_type,
1034                      p2p->num_req_dev_types, p2p->req_dev_types,
1035                      p2p->find_dev_id, p2p->search_delay) < 0)
1036                 return 0;
1037         return 1;
1038 }
1039
1040
1041 void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
1042 {
1043         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find");
1044         eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1045         p2p_clear_timeout(p2p);
1046         if (p2p->state == P2P_SEARCH)
1047                 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1048         p2p_set_state(p2p, P2P_IDLE);
1049         p2p_free_req_dev_types(p2p);
1050         p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1051         p2p->go_neg_peer = NULL;
1052         p2p->sd_peer = NULL;
1053         p2p->invite_peer = NULL;
1054         p2p_stop_listen_for_freq(p2p, freq);
1055 }
1056
1057
1058 void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1059 {
1060         if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
1061                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen "
1062                         "since we are on correct channel for response");
1063                 return;
1064         }
1065         if (p2p->in_listen) {
1066                 p2p->in_listen = 0;
1067                 p2p_clear_timeout(p2p);
1068         }
1069         if (p2p->drv_in_listen) {
1070                 /*
1071                  * The driver may not deliver callback to p2p_listen_end()
1072                  * when the operation gets canceled, so clear the internal
1073                  * variable that is tracking driver state.
1074                  */
1075                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear "
1076                         "drv_in_listen (%d)", p2p->drv_in_listen);
1077                 p2p->drv_in_listen = 0;
1078         }
1079         p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1080 }
1081
1082
1083 void p2p_stop_find(struct p2p_data *p2p)
1084 {
1085         p2p_stop_find_for_freq(p2p, 0);
1086 }
1087
1088
1089 static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq,
1090                                unsigned int pref_freq)
1091 {
1092         if (force_freq || pref_freq) {
1093                 u8 op_reg_class, op_channel;
1094                 unsigned int freq = force_freq ? force_freq : pref_freq;
1095                 if (p2p_freq_to_channel(p2p->cfg->country, freq,
1096                                         &op_reg_class, &op_channel) < 0) {
1097                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1098                                 "P2P: Unsupported frequency %u MHz",
1099                                 freq);
1100                         return -1;
1101                 }
1102                 if (!p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
1103                                            op_channel)) {
1104                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1105                                 "P2P: Frequency %u MHz (oper_class %u "
1106                                 "channel %u) not allowed for P2P",
1107                                 freq, op_reg_class, op_channel);
1108                         return -1;
1109                 }
1110                 p2p->op_reg_class = op_reg_class;
1111                 p2p->op_channel = op_channel;
1112                 if (force_freq) {
1113                         p2p->channels.reg_classes = 1;
1114                         p2p->channels.reg_class[0].channels = 1;
1115                         p2p->channels.reg_class[0].reg_class =
1116                                 p2p->op_reg_class;
1117                         p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
1118                 } else {
1119                         os_memcpy(&p2p->channels, &p2p->cfg->channels,
1120                                   sizeof(struct p2p_channels));
1121                 }
1122         } else {
1123                 u8 op_reg_class, op_channel;
1124
1125                 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1126                     p2p_supported_freq(p2p, p2p->best_freq_overall) &&
1127                     p2p_freq_to_channel(p2p->cfg->country,
1128                                         p2p->best_freq_overall,
1129                                         &op_reg_class, &op_channel) == 0) {
1130                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1131                                 "P2P: Select best overall channel as "
1132                                 "operating channel preference");
1133                         p2p->op_reg_class = op_reg_class;
1134                         p2p->op_channel = op_channel;
1135                 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1136                            p2p_supported_freq(p2p, p2p->best_freq_5) &&
1137                            p2p_freq_to_channel(p2p->cfg->country,
1138                                                p2p->best_freq_5,
1139                                                &op_reg_class, &op_channel) ==
1140                            0) {
1141                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1142                                 "P2P: Select best 5 GHz channel as "
1143                                 "operating channel preference");
1144                         p2p->op_reg_class = op_reg_class;
1145                         p2p->op_channel = op_channel;
1146                 } else if (!p2p->cfg->cfg_op_channel &&
1147                            p2p->best_freq_24 > 0 &&
1148                            p2p_supported_freq(p2p, p2p->best_freq_24) &&
1149                            p2p_freq_to_channel(p2p->cfg->country,
1150                                                p2p->best_freq_24,
1151                                                &op_reg_class, &op_channel) ==
1152                            0) {
1153                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1154                                 "P2P: Select best 2.4 GHz channel as "
1155                                 "operating channel preference");
1156                         p2p->op_reg_class = op_reg_class;
1157                         p2p->op_channel = op_channel;
1158                 } else {
1159                         p2p->op_reg_class = p2p->cfg->op_reg_class;
1160                         p2p->op_channel = p2p->cfg->op_channel;
1161                 }
1162
1163                 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1164                           sizeof(struct p2p_channels));
1165         }
1166         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1167                 "P2P: Own preference for operation channel: "
1168                 "Operating Class %u Channel %u%s",
1169                 p2p->op_reg_class, p2p->op_channel,
1170                 force_freq ? " (forced)" : "");
1171
1172         return 0;
1173 }
1174
1175
1176 static void p2p_set_dev_persistent(struct p2p_device *dev,
1177                                    int persistent_group)
1178 {
1179         switch (persistent_group) {
1180         case 0:
1181                 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1182                                 P2P_DEV_PREFER_PERSISTENT_RECONN);
1183                 break;
1184         case 1:
1185                 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1186                 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1187                 break;
1188         case 2:
1189                 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1190                         P2P_DEV_PREFER_PERSISTENT_RECONN;
1191                 break;
1192         }
1193 }
1194
1195
1196 int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1197                 enum p2p_wps_method wps_method,
1198                 int go_intent, const u8 *own_interface_addr,
1199                 unsigned int force_freq, int persistent_group,
1200                 const u8 *force_ssid, size_t force_ssid_len,
1201                 int pd_before_go_neg, unsigned int pref_freq)
1202 {
1203         struct p2p_device *dev;
1204
1205         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1206                 "P2P: Request to start group negotiation - peer=" MACSTR
1207                 "  GO Intent=%d  Intended Interface Address=" MACSTR
1208                 " wps_method=%d persistent_group=%d pd_before_go_neg=%d",
1209                 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1210                 wps_method, persistent_group, pd_before_go_neg);
1211
1212         if (p2p_prepare_channel(p2p, force_freq, pref_freq) < 0)
1213                 return -1;
1214
1215         dev = p2p_get_device(p2p, peer_addr);
1216         if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
1217                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1218                         "P2P: Cannot connect to unknown P2P Device " MACSTR,
1219                         MAC2STR(peer_addr));
1220                 return -1;
1221         }
1222
1223         if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1224                 if (!(dev->info.dev_capab &
1225                       P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
1226                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1227                                 "P2P: Cannot connect to P2P Device " MACSTR
1228                                 " that is in a group and is not discoverable",
1229                                 MAC2STR(peer_addr));
1230                         return -1;
1231                 }
1232                 if (dev->oper_freq <= 0) {
1233                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1234                                 "P2P: Cannot connect to P2P Device " MACSTR
1235                                 " with incomplete information",
1236                                 MAC2STR(peer_addr));
1237                         return -1;
1238                 }
1239
1240                 /*
1241                  * First, try to connect directly. If the peer does not
1242                  * acknowledge frames, assume it is sleeping and use device
1243                  * discoverability via the GO at that point.
1244                  */
1245         }
1246
1247         p2p->ssid_set = 0;
1248         if (force_ssid) {
1249                 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1250                                   force_ssid, force_ssid_len);
1251                 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1252                 p2p->ssid_len = force_ssid_len;
1253                 p2p->ssid_set = 1;
1254         }
1255
1256         dev->flags &= ~P2P_DEV_NOT_YET_READY;
1257         dev->flags &= ~P2P_DEV_USER_REJECTED;
1258         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1259         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1260         if (pd_before_go_neg)
1261                 dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
1262         else
1263                 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
1264         dev->connect_reqs = 0;
1265         dev->go_neg_req_sent = 0;
1266         dev->go_state = UNKNOWN_GO;
1267         p2p_set_dev_persistent(dev, persistent_group);
1268         p2p->go_intent = go_intent;
1269         os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1270
1271         if (p2p->state != P2P_IDLE)
1272                 p2p_stop_find(p2p);
1273
1274         if (p2p->after_scan_tx) {
1275                 /*
1276                  * We need to drop the pending frame to avoid issues with the
1277                  * new GO Negotiation, e.g., when the pending frame was from a
1278                  * previous attempt at starting a GO Negotiation.
1279                  */
1280                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
1281                         "previous pending Action frame TX that was waiting "
1282                         "for p2p_scan completion");
1283                 os_free(p2p->after_scan_tx);
1284                 p2p->after_scan_tx = NULL;
1285         }
1286
1287         dev->wps_method = wps_method;
1288         dev->status = P2P_SC_SUCCESS;
1289
1290         if (force_freq)
1291                 dev->flags |= P2P_DEV_FORCE_FREQ;
1292         else
1293                 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1294
1295         if (p2p->p2p_scan_running) {
1296                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1297                         "P2P: p2p_scan running - delay connect send");
1298                 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1299                 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1300                 return 0;
1301         }
1302         p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1303
1304         return p2p_connect_send(p2p, dev);
1305 }
1306
1307
1308 int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1309                   enum p2p_wps_method wps_method,
1310                   int go_intent, const u8 *own_interface_addr,
1311                   unsigned int force_freq, int persistent_group,
1312                   const u8 *force_ssid, size_t force_ssid_len,
1313                   unsigned int pref_freq)
1314 {
1315         struct p2p_device *dev;
1316
1317         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1318                 "P2P: Request to authorize group negotiation - peer=" MACSTR
1319                 "  GO Intent=%d  Intended Interface Address=" MACSTR
1320                 " wps_method=%d  persistent_group=%d",
1321                 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1322                 wps_method, persistent_group);
1323
1324         if (p2p_prepare_channel(p2p, force_freq, pref_freq) < 0)
1325                 return -1;
1326
1327         dev = p2p_get_device(p2p, peer_addr);
1328         if (dev == NULL) {
1329                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1330                         "P2P: Cannot authorize unknown P2P Device " MACSTR,
1331                         MAC2STR(peer_addr));
1332                 return -1;
1333         }
1334
1335         p2p->ssid_set = 0;
1336         if (force_ssid) {
1337                 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1338                                   force_ssid, force_ssid_len);
1339                 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1340                 p2p->ssid_len = force_ssid_len;
1341                 p2p->ssid_set = 1;
1342         }
1343
1344         dev->flags &= ~P2P_DEV_NOT_YET_READY;
1345         dev->flags &= ~P2P_DEV_USER_REJECTED;
1346         dev->go_neg_req_sent = 0;
1347         dev->go_state = UNKNOWN_GO;
1348         p2p_set_dev_persistent(dev, persistent_group);
1349         p2p->go_intent = go_intent;
1350         os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1351
1352         dev->wps_method = wps_method;
1353         dev->status = P2P_SC_SUCCESS;
1354
1355         if (force_freq)
1356                 dev->flags |= P2P_DEV_FORCE_FREQ;
1357         else
1358                 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1359
1360         return 0;
1361 }
1362
1363
1364 void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1365                       struct p2p_device *dev, struct p2p_message *msg)
1366 {
1367         os_get_time(&dev->last_seen);
1368
1369         p2p_copy_wps_info(dev, 0, msg);
1370
1371         if (msg->listen_channel) {
1372                 int freq;
1373                 freq = p2p_channel_to_freq((char *) msg->listen_channel,
1374                                            msg->listen_channel[3],
1375                                            msg->listen_channel[4]);
1376                 if (freq < 0) {
1377                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1378                                 "P2P: Unknown peer Listen channel: "
1379                                 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1380                                 msg->listen_channel[0],
1381                                 msg->listen_channel[1],
1382                                 msg->listen_channel[2],
1383                                 msg->listen_channel[3],
1384                                 msg->listen_channel[4]);
1385                 } else {
1386                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
1387                                 "peer " MACSTR " Listen channel: %u -> %u MHz",
1388                                 MAC2STR(dev->info.p2p_device_addr),
1389                                 dev->listen_freq, freq);
1390                         dev->listen_freq = freq;
1391                 }
1392         }
1393
1394         if (msg->wfd_subelems) {
1395                 wpabuf_free(dev->info.wfd_subelems);
1396                 dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
1397         }
1398
1399         if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1400                 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
1401                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1402                         "P2P: Completed device entry based on data from "
1403                         "GO Negotiation Request");
1404         } else {
1405                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1406                         "P2P: Created device entry based on GO Neg Req: "
1407                         MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1408                         "listen_freq=%d",
1409                         MAC2STR(dev->info.p2p_device_addr),
1410                         dev->info.dev_capab, dev->info.group_capab,
1411                         dev->info.device_name, dev->listen_freq);
1412         }
1413
1414         dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1415
1416         if (dev->flags & P2P_DEV_USER_REJECTED) {
1417                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1418                         "P2P: Do not report rejected device");
1419                 return;
1420         }
1421
1422         p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1423                             !(dev->flags & P2P_DEV_REPORTED_ONCE));
1424         dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1425 }
1426
1427
1428 void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1429 {
1430         os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1431         p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1432         os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1433                   p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1434         *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1435 }
1436
1437
1438 int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1439 {
1440         p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
1441         p2p_random(params->passphrase, 8);
1442         return 0;
1443 }
1444
1445
1446 void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1447 {
1448         struct p2p_go_neg_results res;
1449         int go = peer->go_state == LOCAL_GO;
1450         struct p2p_channels intersection;
1451         int freqs;
1452         size_t i, j;
1453
1454         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1455                 "P2P: GO Negotiation with " MACSTR " completed (%s will be "
1456                 "GO)", MAC2STR(peer->info.p2p_device_addr),
1457                 go ? "local end" : "peer");
1458
1459         os_memset(&res, 0, sizeof(res));
1460         res.role_go = go;
1461         os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1462         os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1463         res.wps_method = peer->wps_method;
1464         if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1465                 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1466                         res.persistent_group = 2;
1467                 else
1468                         res.persistent_group = 1;
1469         }
1470
1471         if (go) {
1472                 /* Setup AP mode for WPS provisioning */
1473                 res.freq = p2p_channel_to_freq(p2p->cfg->country,
1474                                                p2p->op_reg_class,
1475                                                p2p->op_channel);
1476                 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1477                 res.ssid_len = p2p->ssid_len;
1478                 p2p_random(res.passphrase, 8);
1479         } else {
1480                 res.freq = peer->oper_freq;
1481                 if (p2p->ssid_len) {
1482                         os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1483                         res.ssid_len = p2p->ssid_len;
1484                 }
1485         }
1486
1487         p2p_channels_intersect(&p2p->channels, &peer->channels,
1488                                &intersection);
1489         freqs = 0;
1490         for (i = 0; i < intersection.reg_classes; i++) {
1491                 struct p2p_reg_class *c = &intersection.reg_class[i];
1492                 if (freqs + 1 == P2P_MAX_CHANNELS)
1493                         break;
1494                 for (j = 0; j < c->channels; j++) {
1495                         int freq;
1496                         if (freqs + 1 == P2P_MAX_CHANNELS)
1497                                 break;
1498                         freq = p2p_channel_to_freq(peer->country, c->reg_class,
1499                                                    c->channel[j]);
1500                         if (freq < 0)
1501                                 continue;
1502                         res.freq_list[freqs++] = freq;
1503                 }
1504         }
1505
1506         res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1507
1508         p2p_clear_timeout(p2p);
1509         p2p->ssid_set = 0;
1510         peer->go_neg_req_sent = 0;
1511         peer->wps_method = WPS_NOT_READY;
1512
1513         p2p_set_state(p2p, P2P_PROVISIONING);
1514         p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1515 }
1516
1517
1518 static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1519                               const u8 *data, size_t len, int rx_freq)
1520 {
1521         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1522                 "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
1523         wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1524
1525         if (len < 1)
1526                 return;
1527
1528         switch (data[0]) {
1529         case P2P_GO_NEG_REQ:
1530                 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1531                 break;
1532         case P2P_GO_NEG_RESP:
1533                 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1534                 break;
1535         case P2P_GO_NEG_CONF:
1536                 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1537                 break;
1538         case P2P_INVITATION_REQ:
1539                 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1540                                            rx_freq);
1541                 break;
1542         case P2P_INVITATION_RESP:
1543                 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1544                 break;
1545         case P2P_PROV_DISC_REQ:
1546                 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1547                 break;
1548         case P2P_PROV_DISC_RESP:
1549                 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1550                 break;
1551         case P2P_DEV_DISC_REQ:
1552                 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1553                 break;
1554         case P2P_DEV_DISC_RESP:
1555                 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1556                 break;
1557         default:
1558                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1559                         "P2P: Unsupported P2P Public Action frame type %d",
1560                         data[0]);
1561                 break;
1562         }
1563 }
1564
1565
1566 static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1567                                  const u8 *sa, const u8 *bssid, const u8 *data,
1568                                  size_t len, int freq)
1569 {
1570         if (len < 1)
1571                 return;
1572
1573         switch (data[0]) {
1574         case WLAN_PA_VENDOR_SPECIFIC:
1575                 data++;
1576                 len--;
1577                 if (len < 3)
1578                         return;
1579                 if (WPA_GET_BE24(data) != OUI_WFA)
1580                         return;
1581
1582                 data += 3;
1583                 len -= 3;
1584                 if (len < 1)
1585                         return;
1586
1587                 if (*data != P2P_OUI_TYPE)
1588                         return;
1589
1590                 p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
1591                 break;
1592         case WLAN_PA_GAS_INITIAL_REQ:
1593                 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1594                 break;
1595         case WLAN_PA_GAS_INITIAL_RESP:
1596                 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1597                 break;
1598         case WLAN_PA_GAS_COMEBACK_REQ:
1599                 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1600                 break;
1601         case WLAN_PA_GAS_COMEBACK_RESP:
1602                 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1603                 break;
1604         }
1605 }
1606
1607
1608 void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1609                    const u8 *bssid, u8 category,
1610                    const u8 *data, size_t len, int freq)
1611 {
1612         if (category == WLAN_ACTION_PUBLIC) {
1613                 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1614                 return;
1615         }
1616
1617         if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1618                 return;
1619
1620         if (len < 4)
1621                 return;
1622
1623         if (WPA_GET_BE24(data) != OUI_WFA)
1624                 return;
1625         data += 3;
1626         len -= 3;
1627
1628         if (*data != P2P_OUI_TYPE)
1629                 return;
1630         data++;
1631         len--;
1632
1633         /* P2P action frame */
1634         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1635                 "P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
1636         wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1637
1638         if (len < 1)
1639                 return;
1640         switch (data[0]) {
1641         case P2P_NOA:
1642                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1643                         "P2P: Received P2P Action - Notice of Absence");
1644                 /* TODO */
1645                 break;
1646         case P2P_PRESENCE_REQ:
1647                 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1648                 break;
1649         case P2P_PRESENCE_RESP:
1650                 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1651                 break;
1652         case P2P_GO_DISC_REQ:
1653                 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1654                 break;
1655         default:
1656                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1657                         "P2P: Received P2P Action - unknown type %u", data[0]);
1658                 break;
1659         }
1660 }
1661
1662
1663 static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1664 {
1665         struct p2p_data *p2p = eloop_ctx;
1666         if (p2p->go_neg_peer == NULL)
1667                 return;
1668         p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1669         p2p->go_neg_peer->status = P2P_SC_SUCCESS;
1670         p2p_connect_send(p2p, p2p->go_neg_peer);
1671 }
1672
1673
1674 static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
1675 {
1676         struct p2p_data *p2p = eloop_ctx;
1677         if (p2p->invite_peer == NULL)
1678                 return;
1679         p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1680         p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
1681 }
1682
1683
1684 static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
1685                                        const u8 *ie, size_t ie_len)
1686 {
1687         struct p2p_message msg;
1688         struct p2p_device *dev;
1689
1690         os_memset(&msg, 0, sizeof(msg));
1691         if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
1692         {
1693                 p2p_parse_free(&msg);
1694                 return; /* not a P2P probe */
1695         }
1696
1697         if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
1698             os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
1699             != 0) {
1700                 /* The Probe Request is not part of P2P Device Discovery. It is
1701                  * not known whether the source address of the frame is the P2P
1702                  * Device Address or P2P Interface Address. Do not add a new
1703                  * peer entry based on this frames.
1704                  */
1705                 p2p_parse_free(&msg);
1706                 return;
1707         }
1708
1709         dev = p2p_get_device(p2p, addr);
1710         if (dev) {
1711                 if (dev->country[0] == 0 && msg.listen_channel)
1712                         os_memcpy(dev->country, msg.listen_channel, 3);
1713                 os_get_time(&dev->last_seen);
1714                 p2p_parse_free(&msg);
1715                 return; /* already known */
1716         }
1717
1718         dev = p2p_create_device(p2p, addr);
1719         if (dev == NULL) {
1720                 p2p_parse_free(&msg);
1721                 return;
1722         }
1723
1724         os_get_time(&dev->last_seen);
1725         dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
1726
1727         if (msg.listen_channel) {
1728                 os_memcpy(dev->country, msg.listen_channel, 3);
1729                 dev->listen_freq = p2p_channel_to_freq(dev->country,
1730                                                        msg.listen_channel[3],
1731                                                        msg.listen_channel[4]);
1732         }
1733
1734         p2p_copy_wps_info(dev, 1, &msg);
1735
1736         if (msg.wfd_subelems) {
1737                 wpabuf_free(dev->info.wfd_subelems);
1738                 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
1739         }
1740
1741         p2p_parse_free(&msg);
1742
1743         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1744                 "P2P: Created device entry based on Probe Req: " MACSTR
1745                 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1746                 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
1747                 dev->info.group_capab, dev->info.device_name,
1748                 dev->listen_freq);
1749 }
1750
1751
1752 struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
1753                                                 const u8 *addr,
1754                                                 struct p2p_message *msg)
1755 {
1756         struct p2p_device *dev;
1757
1758         dev = p2p_get_device(p2p, addr);
1759         if (dev) {
1760                 os_get_time(&dev->last_seen);
1761                 return dev; /* already known */
1762         }
1763
1764         dev = p2p_create_device(p2p, addr);
1765         if (dev == NULL)
1766                 return NULL;
1767
1768         p2p_add_dev_info(p2p, addr, dev, msg);
1769
1770         return dev;
1771 }
1772
1773
1774 static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
1775 {
1776         if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
1777                 return 1;
1778         if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
1779             WPA_GET_BE32(&req_dev_type[2]) == 0 &&
1780             WPA_GET_BE16(&req_dev_type[6]) == 0)
1781                 return 1; /* Category match with wildcard OUI/sub-category */
1782         return 0;
1783 }
1784
1785
1786 int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
1787                         size_t num_req_dev_type)
1788 {
1789         size_t i;
1790         for (i = 0; i < num_req_dev_type; i++) {
1791                 if (dev_type_match(dev_type, req_dev_type[i]))
1792                         return 1;
1793         }
1794         return 0;
1795 }
1796
1797
1798 /**
1799  * p2p_match_dev_type - Match local device type with requested type
1800  * @p2p: P2P module context from p2p_init()
1801  * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1802  * Returns: 1 on match, 0 on mismatch
1803  *
1804  * This function can be used to match the Requested Device Type attribute in
1805  * WPS IE with the local device types for deciding whether to reply to a Probe
1806  * Request frame.
1807  */
1808 int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
1809 {
1810         struct wps_parse_attr attr;
1811         size_t i;
1812
1813         if (wps_parse_msg(wps, &attr))
1814                 return 1; /* assume no Requested Device Type attributes */
1815
1816         if (attr.num_req_dev_type == 0)
1817                 return 1; /* no Requested Device Type attributes -> match */
1818
1819         if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
1820                                 attr.num_req_dev_type))
1821                 return 1; /* Own Primary Device Type matches */
1822
1823         for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
1824                 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
1825                                         attr.req_dev_type,
1826                                         attr.num_req_dev_type))
1827                 return 1; /* Own Secondary Device Type matches */
1828
1829         /* No matching device type found */
1830         return 0;
1831 }
1832
1833
1834 struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
1835 {
1836         struct wpabuf *buf;
1837         u8 *len;
1838         int pw_id = -1;
1839         size_t extra = 0;
1840
1841 #ifdef CONFIG_WIFI_DISPLAY
1842         if (p2p->wfd_ie_probe_resp)
1843                 extra = wpabuf_len(p2p->wfd_ie_probe_resp);
1844 #endif /* CONFIG_WIFI_DISPLAY */
1845
1846         buf = wpabuf_alloc(1000 + extra);
1847         if (buf == NULL)
1848                 return NULL;
1849
1850         if (p2p->go_neg_peer) {
1851                 /* Advertise immediate availability of WPS credential */
1852                 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
1853         }
1854
1855         p2p_build_wps_ie(p2p, buf, pw_id, 1);
1856
1857 #ifdef CONFIG_WIFI_DISPLAY
1858         if (p2p->wfd_ie_probe_resp)
1859                 wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
1860 #endif /* CONFIG_WIFI_DISPLAY */
1861
1862         /* P2P IE */
1863         len = p2p_buf_add_ie_hdr(buf);
1864         p2p_buf_add_capability(buf, p2p->dev_capab &
1865                                ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
1866         if (p2p->ext_listen_interval)
1867                 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
1868                                               p2p->ext_listen_interval);
1869         p2p_buf_add_device_info(buf, p2p, NULL);
1870         p2p_buf_update_ie_hdr(buf, len);
1871
1872         return buf;
1873 }
1874
1875
1876 static int is_11b(u8 rate)
1877 {
1878         return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16;
1879 }
1880
1881
1882 static int supp_rates_11b_only(struct ieee802_11_elems *elems)
1883 {
1884         int num_11b = 0, num_others = 0;
1885         int i;
1886
1887         if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL)
1888                 return 0;
1889
1890         for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) {
1891                 if (is_11b(elems->supp_rates[i]))
1892                         num_11b++;
1893                 else
1894                         num_others++;
1895         }
1896
1897         for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len;
1898              i++) {
1899                 if (is_11b(elems->ext_supp_rates[i]))
1900                         num_11b++;
1901                 else
1902                         num_others++;
1903         }
1904
1905         return num_11b > 0 && num_others == 0;
1906 }
1907
1908
1909 static enum p2p_probe_req_status
1910 p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
1911                 const u8 *bssid, const u8 *ie, size_t ie_len)
1912 {
1913         struct ieee802_11_elems elems;
1914         struct wpabuf *buf;
1915         struct ieee80211_mgmt *resp;
1916         struct p2p_message msg;
1917         struct wpabuf *ies;
1918
1919         if (!p2p->in_listen || !p2p->drv_in_listen) {
1920                 /* not in Listen state - ignore Probe Request */
1921                 return P2P_PREQ_NOT_LISTEN;
1922         }
1923
1924         if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
1925             ParseFailed) {
1926                 /* Ignore invalid Probe Request frames */
1927                 return P2P_PREQ_MALFORMED;
1928         }
1929
1930         if (elems.p2p == NULL) {
1931                 /* not a P2P probe - ignore it */
1932                 return P2P_PREQ_NOT_P2P;
1933         }
1934
1935         if (dst && !is_broadcast_ether_addr(dst) &&
1936             os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
1937                 /* Not sent to the broadcast address or our P2P Device Address
1938                  */
1939                 return P2P_PREQ_NOT_PROCESSED;
1940         }
1941
1942         if (bssid && !is_broadcast_ether_addr(bssid)) {
1943                 /* Not sent to the Wildcard BSSID */
1944                 return P2P_PREQ_NOT_PROCESSED;
1945         }
1946
1947         if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
1948             os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
1949             0) {
1950                 /* not using P2P Wildcard SSID - ignore */
1951                 return P2P_PREQ_NOT_PROCESSED;
1952         }
1953
1954         if (supp_rates_11b_only(&elems)) {
1955                 /* Indicates support for 11b rates only */
1956                 return P2P_PREQ_NOT_P2P;
1957         }
1958
1959         os_memset(&msg, 0, sizeof(msg));
1960         if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
1961                 /* Could not parse P2P attributes */
1962                 return P2P_PREQ_NOT_P2P;
1963         }
1964
1965         if (msg.device_id &&
1966             os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
1967                 /* Device ID did not match */
1968                 p2p_parse_free(&msg);
1969                 return P2P_PREQ_NOT_PROCESSED;
1970         }
1971
1972         /* Check Requested Device Type match */
1973         if (msg.wps_attributes &&
1974             !p2p_match_dev_type(p2p, msg.wps_attributes)) {
1975                 /* No match with Requested Device Type */
1976                 p2p_parse_free(&msg);
1977                 return P2P_PREQ_NOT_PROCESSED;
1978         }
1979         p2p_parse_free(&msg);
1980
1981         if (!p2p->cfg->send_probe_resp) {
1982                 /* Response generated elsewhere */
1983                 return P2P_PREQ_NOT_PROCESSED;
1984         }
1985
1986         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1987                 "P2P: Reply to P2P Probe Request in Listen state");
1988
1989         /*
1990          * We do not really have a specific BSS that this frame is advertising,
1991          * so build a frame that has some information in valid format. This is
1992          * really only used for discovery purposes, not to learn exact BSS
1993          * parameters.
1994          */
1995         ies = p2p_build_probe_resp_ies(p2p);
1996         if (ies == NULL)
1997                 return P2P_PREQ_NOT_PROCESSED;
1998
1999         buf = wpabuf_alloc(200 + wpabuf_len(ies));
2000         if (buf == NULL) {
2001                 wpabuf_free(ies);
2002                 return P2P_PREQ_NOT_PROCESSED;
2003         }
2004
2005         resp = NULL;
2006         resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
2007
2008         resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
2009                                            (WLAN_FC_STYPE_PROBE_RESP << 4));
2010         os_memcpy(resp->da, addr, ETH_ALEN);
2011         os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
2012         os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
2013         resp->u.probe_resp.beacon_int = host_to_le16(100);
2014         /* hardware or low-level driver will setup seq_ctrl and timestamp */
2015         resp->u.probe_resp.capab_info =
2016                 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
2017                              WLAN_CAPABILITY_PRIVACY |
2018                              WLAN_CAPABILITY_SHORT_SLOT_TIME);
2019
2020         wpabuf_put_u8(buf, WLAN_EID_SSID);
2021         wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
2022         wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
2023
2024         wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
2025         wpabuf_put_u8(buf, 8);
2026         wpabuf_put_u8(buf, (60 / 5) | 0x80);
2027         wpabuf_put_u8(buf, 90 / 5);
2028         wpabuf_put_u8(buf, (120 / 5) | 0x80);
2029         wpabuf_put_u8(buf, 180 / 5);
2030         wpabuf_put_u8(buf, (240 / 5) | 0x80);
2031         wpabuf_put_u8(buf, 360 / 5);
2032         wpabuf_put_u8(buf, 480 / 5);
2033         wpabuf_put_u8(buf, 540 / 5);
2034
2035         wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
2036         wpabuf_put_u8(buf, 1);
2037         wpabuf_put_u8(buf, p2p->cfg->channel);
2038
2039         wpabuf_put_buf(buf, ies);
2040         wpabuf_free(ies);
2041
2042         p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
2043
2044         wpabuf_free(buf);
2045
2046         return P2P_PREQ_NOT_PROCESSED;
2047 }
2048
2049
2050 enum p2p_probe_req_status
2051 p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
2052                  const u8 *bssid, const u8 *ie, size_t ie_len)
2053 {
2054         enum p2p_probe_req_status res;
2055
2056         p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2057
2058         res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
2059
2060         if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2061             p2p->go_neg_peer &&
2062             os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
2063             == 0) {
2064                 /* Received a Probe Request from GO Negotiation peer */
2065                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2066                         "P2P: Found GO Negotiation peer - try to start GO "
2067                         "negotiation from timeout");
2068                 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
2069                 return P2P_PREQ_PROCESSED;
2070         }
2071
2072         if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2073             p2p->invite_peer &&
2074             os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
2075             == 0) {
2076                 /* Received a Probe Request from Invite peer */
2077                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2078                         "P2P: Found Invite peer - try to start Invite from "
2079                         "timeout");
2080                 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
2081                 return P2P_PREQ_PROCESSED;
2082         }
2083
2084         return res;
2085 }
2086
2087
2088 static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2089                                     u8 *buf, size_t len, struct wpabuf *p2p_ie)
2090 {
2091         struct wpabuf *tmp;
2092         u8 *lpos;
2093         size_t tmplen;
2094         int res;
2095         u8 group_capab;
2096
2097         if (p2p_ie == NULL)
2098                 return 0; /* WLAN AP is not a P2P manager */
2099
2100         /*
2101          * (Re)Association Request - P2P IE
2102          * P2P Capability attribute (shall be present)
2103          * P2P Interface attribute (present if concurrent device and
2104          *      P2P Management is enabled)
2105          */
2106         tmp = wpabuf_alloc(200);
2107         if (tmp == NULL)
2108                 return -1;
2109
2110         lpos = p2p_buf_add_ie_hdr(tmp);
2111         group_capab = 0;
2112         if (p2p->num_groups > 0) {
2113                 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2114                 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2115                     (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2116                     p2p->cross_connect)
2117                         group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2118         }
2119         p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2120         if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2121             (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2122                 p2p_buf_add_p2p_interface(tmp, p2p);
2123         p2p_buf_update_ie_hdr(tmp, lpos);
2124
2125         tmplen = wpabuf_len(tmp);
2126         if (tmplen > len)
2127                 res = -1;
2128         else {
2129                 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2130                 res = tmplen;
2131         }
2132         wpabuf_free(tmp);
2133
2134         return res;
2135 }
2136
2137
2138 int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2139                      size_t len, int p2p_group, struct wpabuf *p2p_ie)
2140 {
2141         struct wpabuf *tmp;
2142         u8 *lpos;
2143         struct p2p_device *peer;
2144         size_t tmplen;
2145         int res;
2146         size_t extra = 0;
2147
2148         if (!p2p_group)
2149                 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2150
2151 #ifdef CONFIG_WIFI_DISPLAY
2152         if (p2p->wfd_ie_assoc_req)
2153                 extra = wpabuf_len(p2p->wfd_ie_assoc_req);
2154 #endif /* CONFIG_WIFI_DISPLAY */
2155
2156         /*
2157          * (Re)Association Request - P2P IE
2158          * P2P Capability attribute (shall be present)
2159          * Extended Listen Timing (may be present)
2160          * P2P Device Info attribute (shall be present)
2161          */
2162         tmp = wpabuf_alloc(200 + extra);
2163         if (tmp == NULL)
2164                 return -1;
2165
2166 #ifdef CONFIG_WIFI_DISPLAY
2167         if (p2p->wfd_ie_assoc_req)
2168                 wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
2169 #endif /* CONFIG_WIFI_DISPLAY */
2170
2171         peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2172
2173         lpos = p2p_buf_add_ie_hdr(tmp);
2174         p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2175         if (p2p->ext_listen_interval)
2176                 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2177                                               p2p->ext_listen_interval);
2178         p2p_buf_add_device_info(tmp, p2p, peer);
2179         p2p_buf_update_ie_hdr(tmp, lpos);
2180
2181         tmplen = wpabuf_len(tmp);
2182         if (tmplen > len)
2183                 res = -1;
2184         else {
2185                 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2186                 res = tmplen;
2187         }
2188         wpabuf_free(tmp);
2189
2190         return res;
2191 }
2192
2193
2194 int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2195 {
2196         struct wpabuf *p2p_ie;
2197         int ret;
2198
2199         p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2200         if (p2p_ie == NULL)
2201                 return 0;
2202
2203         ret = p2p_attr_text(p2p_ie, buf, end);
2204         wpabuf_free(p2p_ie);
2205         return ret;
2206 }
2207
2208
2209 int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
2210 {
2211         struct p2p_message msg;
2212
2213         os_memset(&msg, 0, sizeof(msg));
2214         if (p2p_parse_p2p_ie(p2p_ie, &msg))
2215                 return -1;
2216
2217         if (msg.p2p_device_addr) {
2218                 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2219                 return 0;
2220         } else if (msg.device_id) {
2221                 os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2222                 return 0;
2223         }
2224         return -1;
2225 }
2226
2227
2228 int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2229 {
2230         struct wpabuf *p2p_ie;
2231         int ret;
2232
2233         p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2234                                              P2P_IE_VENDOR_TYPE);
2235         if (p2p_ie == NULL)
2236                 return -1;
2237         ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
2238         wpabuf_free(p2p_ie);
2239         return ret;
2240 }
2241
2242
2243 static void p2p_clear_go_neg(struct p2p_data *p2p)
2244 {
2245         p2p->go_neg_peer = NULL;
2246         p2p_clear_timeout(p2p);
2247         p2p_set_state(p2p, P2P_IDLE);
2248 }
2249
2250
2251 void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2252 {
2253         if (p2p->go_neg_peer == NULL) {
2254                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2255                         "P2P: No pending Group Formation - "
2256                         "ignore WPS registration success notification");
2257                 return; /* No pending Group Formation */
2258         }
2259
2260         if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2261             0) {
2262                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2263                         "P2P: Ignore WPS registration success notification "
2264                         "for " MACSTR " (GO Negotiation peer " MACSTR ")",
2265                         MAC2STR(mac_addr),
2266                         MAC2STR(p2p->go_neg_peer->intended_addr));
2267                 return; /* Ignore unexpected peer address */
2268         }
2269
2270         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2271                 "P2P: Group Formation completed successfully with " MACSTR,
2272                 MAC2STR(mac_addr));
2273
2274         p2p_clear_go_neg(p2p);
2275 }
2276
2277
2278 void p2p_group_formation_failed(struct p2p_data *p2p)
2279 {
2280         if (p2p->go_neg_peer == NULL) {
2281                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2282                         "P2P: No pending Group Formation - "
2283                         "ignore group formation failure notification");
2284                 return; /* No pending Group Formation */
2285         }
2286
2287         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2288                 "P2P: Group Formation failed with " MACSTR,
2289                 MAC2STR(p2p->go_neg_peer->intended_addr));
2290
2291         p2p_clear_go_neg(p2p);
2292 }
2293
2294
2295 struct p2p_data * p2p_init(const struct p2p_config *cfg)
2296 {
2297         struct p2p_data *p2p;
2298
2299         if (cfg->max_peers < 1)
2300                 return NULL;
2301
2302         p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2303         if (p2p == NULL)
2304                 return NULL;
2305         p2p->cfg = (struct p2p_config *) (p2p + 1);
2306         os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2307         if (cfg->dev_name)
2308                 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2309         if (cfg->manufacturer)
2310                 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2311         if (cfg->model_name)
2312                 p2p->cfg->model_name = os_strdup(cfg->model_name);
2313         if (cfg->model_number)
2314                 p2p->cfg->model_number = os_strdup(cfg->model_number);
2315         if (cfg->serial_number)
2316                 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
2317         if (cfg->pref_chan) {
2318                 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
2319                                                 sizeof(struct p2p_channel));
2320                 if (p2p->cfg->pref_chan) {
2321                         os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
2322                                   cfg->num_pref_chan *
2323                                   sizeof(struct p2p_channel));
2324                 } else
2325                         p2p->cfg->num_pref_chan = 0;
2326         }
2327
2328         p2p->min_disc_int = 1;
2329         p2p->max_disc_int = 3;
2330
2331         os_get_random(&p2p->next_tie_breaker, 1);
2332         p2p->next_tie_breaker &= 0x01;
2333         if (cfg->sd_request)
2334                 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2335         p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2336         if (cfg->concurrent_operations)
2337                 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2338         p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2339
2340         dl_list_init(&p2p->devices);
2341
2342         eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
2343                                p2p_expiration_timeout, p2p, NULL);
2344
2345         p2p->go_timeout = 100;
2346         p2p->client_timeout = 20;
2347
2348         return p2p;
2349 }
2350
2351
2352 void p2p_deinit(struct p2p_data *p2p)
2353 {
2354 #ifdef CONFIG_WIFI_DISPLAY
2355         wpabuf_free(p2p->wfd_ie_beacon);
2356         wpabuf_free(p2p->wfd_ie_probe_req);
2357         wpabuf_free(p2p->wfd_ie_probe_resp);
2358         wpabuf_free(p2p->wfd_ie_assoc_req);
2359         wpabuf_free(p2p->wfd_ie_invitation);
2360         wpabuf_free(p2p->wfd_ie_prov_disc_req);
2361         wpabuf_free(p2p->wfd_ie_prov_disc_resp);
2362         wpabuf_free(p2p->wfd_ie_go_neg);
2363         wpabuf_free(p2p->wfd_dev_info);
2364         wpabuf_free(p2p->wfd_assoc_bssid);
2365         wpabuf_free(p2p->wfd_coupled_sink_info);
2366 #endif /* CONFIG_WIFI_DISPLAY */
2367
2368         eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
2369         eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
2370         eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2371         p2p_flush(p2p);
2372         p2p_free_req_dev_types(p2p);
2373         os_free(p2p->cfg->dev_name);
2374         os_free(p2p->cfg->manufacturer);
2375         os_free(p2p->cfg->model_name);
2376         os_free(p2p->cfg->model_number);
2377         os_free(p2p->cfg->serial_number);
2378         os_free(p2p->cfg->pref_chan);
2379         os_free(p2p->groups);
2380         wpabuf_free(p2p->sd_resp);
2381         os_free(p2p->after_scan_tx);
2382         p2p_remove_wps_vendor_extensions(p2p);
2383         os_free(p2p);
2384 }
2385
2386
2387 void p2p_flush(struct p2p_data *p2p)
2388 {
2389         struct p2p_device *dev, *prev;
2390         p2p_stop_find(p2p);
2391         dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
2392                               list) {
2393                 dl_list_del(&dev->list);
2394                 p2p_device_free(p2p, dev);
2395         }
2396         p2p_free_sd_queries(p2p);
2397         os_free(p2p->after_scan_tx);
2398         p2p->after_scan_tx = NULL;
2399 }
2400
2401
2402 int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
2403 {
2404         struct p2p_device *dev;
2405
2406         dev = p2p_get_device(p2p, addr);
2407         if (dev == NULL)
2408                 return -1;
2409
2410         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR,
2411                 MAC2STR(addr));
2412
2413         if (p2p->go_neg_peer == dev)
2414                 p2p->go_neg_peer = NULL;
2415
2416         dev->wps_method = WPS_NOT_READY;
2417         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
2418         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
2419
2420         /* Check if after_scan_tx is for this peer. If so free it */
2421         if (p2p->after_scan_tx &&
2422             os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
2423                 os_free(p2p->after_scan_tx);
2424                 p2p->after_scan_tx = NULL;
2425         }
2426
2427         return 0;
2428 }
2429
2430
2431 int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
2432 {
2433         os_free(p2p->cfg->dev_name);
2434         if (dev_name) {
2435                 p2p->cfg->dev_name = os_strdup(dev_name);
2436                 if (p2p->cfg->dev_name == NULL)
2437                         return -1;
2438         } else
2439                 p2p->cfg->dev_name = NULL;
2440         return 0;
2441 }
2442
2443
2444 int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
2445 {
2446         os_free(p2p->cfg->manufacturer);
2447         p2p->cfg->manufacturer = NULL;
2448         if (manufacturer) {
2449                 p2p->cfg->manufacturer = os_strdup(manufacturer);
2450                 if (p2p->cfg->manufacturer == NULL)
2451                         return -1;
2452         }
2453
2454         return 0;
2455 }
2456
2457
2458 int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
2459 {
2460         os_free(p2p->cfg->model_name);
2461         p2p->cfg->model_name = NULL;
2462         if (model_name) {
2463                 p2p->cfg->model_name = os_strdup(model_name);
2464                 if (p2p->cfg->model_name == NULL)
2465                         return -1;
2466         }
2467
2468         return 0;
2469 }
2470
2471
2472 int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
2473 {
2474         os_free(p2p->cfg->model_number);
2475         p2p->cfg->model_number = NULL;
2476         if (model_number) {
2477                 p2p->cfg->model_number = os_strdup(model_number);
2478                 if (p2p->cfg->model_number == NULL)
2479                         return -1;
2480         }
2481
2482         return 0;
2483 }
2484
2485
2486 int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
2487 {
2488         os_free(p2p->cfg->serial_number);
2489         p2p->cfg->serial_number = NULL;
2490         if (serial_number) {
2491                 p2p->cfg->serial_number = os_strdup(serial_number);
2492                 if (p2p->cfg->serial_number == NULL)
2493                         return -1;
2494         }
2495
2496         return 0;
2497 }
2498
2499
2500 void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
2501 {
2502         p2p->cfg->config_methods = config_methods;
2503 }
2504
2505
2506 void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
2507 {
2508         os_memcpy(p2p->cfg->uuid, uuid, 16);
2509 }
2510
2511
2512 int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
2513 {
2514         os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
2515         return 0;
2516 }
2517
2518
2519 int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
2520                           size_t num_dev_types)
2521 {
2522         if (num_dev_types > P2P_SEC_DEVICE_TYPES)
2523                 num_dev_types = P2P_SEC_DEVICE_TYPES;
2524         p2p->cfg->num_sec_dev_types = num_dev_types;
2525         os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
2526         return 0;
2527 }
2528
2529
2530 void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
2531 {
2532         int i;
2533
2534         for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2535                 wpabuf_free(p2p->wps_vendor_ext[i]);
2536                 p2p->wps_vendor_ext[i] = NULL;
2537         }
2538 }
2539
2540
2541 int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
2542                                  const struct wpabuf *vendor_ext)
2543 {
2544         int i;
2545
2546         if (vendor_ext == NULL)
2547                 return -1;
2548
2549         for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2550                 if (p2p->wps_vendor_ext[i] == NULL)
2551                         break;
2552         }
2553         if (i >= P2P_MAX_WPS_VENDOR_EXT)
2554                 return -1;
2555
2556         p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
2557         if (p2p->wps_vendor_ext[i] == NULL)
2558                 return -1;
2559
2560         return 0;
2561 }
2562
2563
2564 int p2p_set_country(struct p2p_data *p2p, const char *country)
2565 {
2566         os_memcpy(p2p->cfg->country, country, 3);
2567         return 0;
2568 }
2569
2570
2571 void p2p_continue_find(struct p2p_data *p2p)
2572 {
2573         struct p2p_device *dev;
2574         p2p_set_state(p2p, P2P_SEARCH);
2575         dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2576                 if (dev->flags & P2P_DEV_SD_SCHEDULE) {
2577                         if (p2p_start_sd(p2p, dev) == 0)
2578                                 return;
2579                         else
2580                                 break;
2581                 } else if (dev->req_config_methods &&
2582                            !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
2583                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2584                                 "pending Provision Discovery Request to "
2585                                 MACSTR " (config methods 0x%x)",
2586                                 MAC2STR(dev->info.p2p_device_addr),
2587                                 dev->req_config_methods);
2588                         if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
2589                                 return;
2590                 }
2591         }
2592
2593         p2p_listen_in_find(p2p);
2594 }
2595
2596
2597 static void p2p_sd_cb(struct p2p_data *p2p, int success)
2598 {
2599         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2600                 "P2P: Service Discovery Query TX callback: success=%d",
2601                 success);
2602         p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2603
2604         if (!success) {
2605                 if (p2p->sd_peer) {
2606                         p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2607                         p2p->sd_peer = NULL;
2608                 }
2609                 p2p_continue_find(p2p);
2610                 return;
2611         }
2612
2613         if (p2p->sd_peer == NULL) {
2614                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2615                         "P2P: No SD peer entry known");
2616                 p2p_continue_find(p2p);
2617                 return;
2618         }
2619
2620         /* Wait for response from the peer */
2621         p2p_set_state(p2p, P2P_SD_DURING_FIND);
2622         p2p_set_timeout(p2p, 0, 200000);
2623 }
2624
2625
2626 /**
2627  * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
2628  * @p2p: P2P module context from p2p_init()
2629  */
2630 static void p2p_retry_pd(struct p2p_data *p2p)
2631 {
2632         struct p2p_device *dev;
2633
2634         if (p2p->state != P2P_IDLE)
2635                 return;
2636
2637         /*
2638          * Retry the prov disc req attempt only for the peer that the user had
2639          * requested for and provided a join has not been initiated on it
2640          * in the meantime.
2641          */
2642
2643         dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2644                 if (os_memcmp(p2p->pending_pd_devaddr,
2645                               dev->info.p2p_device_addr, ETH_ALEN) != 0)
2646                         continue;
2647                 if (!dev->req_config_methods)
2648                         continue;
2649                 if (dev->flags & P2P_DEV_PD_FOR_JOIN)
2650                         continue;
2651
2652                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2653                         "pending Provision Discovery Request to "
2654                         MACSTR " (config methods 0x%x)",
2655                         MAC2STR(dev->info.p2p_device_addr),
2656                         dev->req_config_methods);
2657                 p2p_send_prov_disc_req(p2p, dev, 0, 0);
2658                 return;
2659         }
2660 }
2661
2662
2663 static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
2664 {
2665         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2666                 "P2P: Provision Discovery Request TX callback: success=%d",
2667                 success);
2668
2669         /*
2670          * Postpone resetting the pending action state till after we actually
2671          * time out. This allows us to take some action like notifying any
2672          * interested parties about no response to the request.
2673          *
2674          * When the timer (below) goes off we check in IDLE, SEARCH, or
2675          * LISTEN_ONLY state, which are the only allowed states to issue a PD
2676          * requests in, if this was still pending and then raise notification.
2677          */
2678
2679         if (!success) {
2680                 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2681
2682                 if (p2p->user_initiated_pd &&
2683                     (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
2684                 {
2685                         /* Retry request from timeout to avoid busy loops */
2686                         p2p->pending_action_state = P2P_PENDING_PD;
2687                         p2p_set_timeout(p2p, 0, 50000);
2688                 } else if (p2p->state != P2P_IDLE)
2689                         p2p_continue_find(p2p);
2690                 else if (p2p->user_initiated_pd) {
2691                         p2p->pending_action_state = P2P_PENDING_PD;
2692                         p2p_set_timeout(p2p, 0, 300000);
2693                 }
2694                 return;
2695         }
2696
2697         /*
2698          * This postponing, of resetting pending_action_state, needs to be
2699          * done only for user initiated PD requests and not internal ones.
2700          */
2701         if (p2p->user_initiated_pd)
2702                 p2p->pending_action_state = P2P_PENDING_PD;
2703         else
2704                 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2705
2706         /* Wait for response from the peer */
2707         if (p2p->state == P2P_SEARCH)
2708                 p2p_set_state(p2p, P2P_PD_DURING_FIND);
2709         p2p_set_timeout(p2p, 0, 200000);
2710 }
2711
2712
2713 int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
2714                          int level, const u8 *ies, size_t ies_len)
2715 {
2716         p2p_add_device(p2p, bssid, freq, level, ies, ies_len, 1);
2717
2718         return 0;
2719 }
2720
2721
2722 void p2p_scan_res_handled(struct p2p_data *p2p)
2723 {
2724         if (!p2p->p2p_scan_running) {
2725                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
2726                         "running, but scan results received");
2727         }
2728         p2p->p2p_scan_running = 0;
2729         eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2730
2731         if (p2p_run_after_scan(p2p))
2732                 return;
2733         if (p2p->state == P2P_SEARCH)
2734                 p2p_continue_find(p2p);
2735 }
2736
2737
2738 void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id)
2739 {
2740         u8 *len;
2741
2742 #ifdef CONFIG_WIFI_DISPLAY
2743         if (p2p->wfd_ie_probe_req)
2744                 wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
2745 #endif /* CONFIG_WIFI_DISPLAY */
2746
2747         len = p2p_buf_add_ie_hdr(ies);
2748         p2p_buf_add_capability(ies, p2p->dev_capab &
2749                                ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
2750         if (dev_id)
2751                 p2p_buf_add_device_id(ies, dev_id);
2752         if (p2p->cfg->reg_class && p2p->cfg->channel)
2753                 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
2754                                            p2p->cfg->reg_class,
2755                                            p2p->cfg->channel);
2756         if (p2p->ext_listen_interval)
2757                 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
2758                                               p2p->ext_listen_interval);
2759         /* TODO: p2p_buf_add_operating_channel() if GO */
2760         p2p_buf_update_ie_hdr(ies, len);
2761 }
2762
2763
2764 size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
2765 {
2766         size_t len = 100;
2767
2768 #ifdef CONFIG_WIFI_DISPLAY
2769         if (p2p && p2p->wfd_ie_probe_req)
2770                 len += wpabuf_len(p2p->wfd_ie_probe_req);
2771 #endif /* CONFIG_WIFI_DISPLAY */
2772
2773         return len;
2774 }
2775
2776
2777 int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
2778 {
2779         return p2p_attr_text(p2p_ie, buf, end);
2780 }
2781
2782
2783 static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
2784 {
2785         struct p2p_device *dev = p2p->go_neg_peer;
2786
2787         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2788                 "P2P: GO Negotiation Request TX callback: success=%d",
2789                 success);
2790
2791         if (dev == NULL) {
2792                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2793                         "P2P: No pending GO Negotiation");
2794                 return;
2795         }
2796
2797         if (success) {
2798                 if (dev->flags & P2P_DEV_USER_REJECTED) {
2799                         p2p_set_state(p2p, P2P_IDLE);
2800                         return;
2801                 }
2802         } else if (dev->go_neg_req_sent) {
2803                 /* Cancel the increment from p2p_connect_send() on failure */
2804                 dev->go_neg_req_sent--;
2805         }
2806
2807         if (!success &&
2808             (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
2809             !is_zero_ether_addr(dev->member_in_go_dev)) {
2810                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2811                         "P2P: Peer " MACSTR " did not acknowledge request - "
2812                         "try to use device discoverability through its GO",
2813                         MAC2STR(dev->info.p2p_device_addr));
2814                 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2815                 p2p_send_dev_disc_req(p2p, dev);
2816                 return;
2817         }
2818
2819         /*
2820          * Use P2P find, if needed, to find the other device from its listen
2821          * channel.
2822          */
2823         p2p_set_state(p2p, P2P_CONNECT);
2824         p2p_set_timeout(p2p, 0, success ? 200000 : 100000);
2825 }
2826
2827
2828 static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
2829 {
2830         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2831                 "P2P: GO Negotiation Response TX callback: success=%d",
2832                 success);
2833         if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
2834                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2835                         "P2P: Ignore TX callback event - GO Negotiation is "
2836                         "not running anymore");
2837                 return;
2838         }
2839         p2p_set_state(p2p, P2P_CONNECT);
2840         p2p_set_timeout(p2p, 0, 250000);
2841 }
2842
2843
2844 static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
2845 {
2846         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2847                 "P2P: GO Negotiation Response (failure) TX callback: "
2848                 "success=%d", success);
2849         if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
2850                 p2p_go_neg_failed(p2p, p2p->go_neg_peer,
2851                                   p2p->go_neg_peer->status);
2852         }
2853 }
2854
2855
2856 static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
2857                                enum p2p_send_action_result result)
2858 {
2859         struct p2p_device *dev;
2860
2861         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2862                 "P2P: GO Negotiation Confirm TX callback: result=%d",
2863                 result);
2864         p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2865         if (result == P2P_SEND_ACTION_FAILED) {
2866                 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2867                 return;
2868         }
2869         if (result == P2P_SEND_ACTION_NO_ACK) {
2870                 /*
2871                  * It looks like the TX status for GO Negotiation Confirm is
2872                  * often showing failure even when the peer has actually
2873                  * received the frame. Since the peer may change channels
2874                  * immediately after having received the frame, we may not see
2875                  * an Ack for retries, so just dropping a single frame may
2876                  * trigger this. To allow the group formation to succeed if the
2877                  * peer did indeed receive the frame, continue regardless of
2878                  * the TX status.
2879                  */
2880                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2881                         "P2P: Assume GO Negotiation Confirm TX was actually "
2882                         "received by the peer even though Ack was not "
2883                         "reported");
2884         }
2885
2886         dev = p2p->go_neg_peer;
2887         if (dev == NULL)
2888                 return;
2889
2890         p2p_go_complete(p2p, dev);
2891 }
2892
2893
2894 void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
2895                         const u8 *src, const u8 *bssid,
2896                         enum p2p_send_action_result result)
2897 {
2898         enum p2p_pending_action_state state;
2899         int success;
2900
2901         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2902                 "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
2903                 " src=" MACSTR " bssid=" MACSTR " result=%d",
2904                 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
2905                 MAC2STR(bssid), result);
2906         success = result == P2P_SEND_ACTION_SUCCESS;
2907         state = p2p->pending_action_state;
2908         p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2909         switch (state) {
2910         case P2P_NO_PENDING_ACTION:
2911                 break;
2912         case P2P_PENDING_GO_NEG_REQUEST:
2913                 p2p_go_neg_req_cb(p2p, success);
2914                 break;
2915         case P2P_PENDING_GO_NEG_RESPONSE:
2916                 p2p_go_neg_resp_cb(p2p, success);
2917                 break;
2918         case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
2919                 p2p_go_neg_resp_failure_cb(p2p, success);
2920                 break;
2921         case P2P_PENDING_GO_NEG_CONFIRM:
2922                 p2p_go_neg_conf_cb(p2p, result);
2923                 break;
2924         case P2P_PENDING_SD:
2925                 p2p_sd_cb(p2p, success);
2926                 break;
2927         case P2P_PENDING_PD:
2928                 p2p_prov_disc_cb(p2p, success);
2929                 break;
2930         case P2P_PENDING_INVITATION_REQUEST:
2931                 p2p_invitation_req_cb(p2p, success);
2932                 break;
2933         case P2P_PENDING_INVITATION_RESPONSE:
2934                 p2p_invitation_resp_cb(p2p, success);
2935                 break;
2936         case P2P_PENDING_DEV_DISC_REQUEST:
2937                 p2p_dev_disc_req_cb(p2p, success);
2938                 break;
2939         case P2P_PENDING_DEV_DISC_RESPONSE:
2940                 p2p_dev_disc_resp_cb(p2p, success);
2941                 break;
2942         case P2P_PENDING_GO_DISC_REQ:
2943                 p2p_go_disc_req_cb(p2p, success);
2944                 break;
2945         }
2946 }
2947
2948
2949 void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
2950                    unsigned int duration)
2951 {
2952         if (freq == p2p->pending_client_disc_freq) {
2953                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2954                         "P2P: Client discoverability remain-awake completed");
2955                 p2p->pending_client_disc_freq = 0;
2956                 return;
2957         }
2958
2959         if (freq != p2p->pending_listen_freq) {
2960                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2961                         "P2P: Unexpected listen callback for freq=%u "
2962                         "duration=%u (pending_listen_freq=%u)",
2963                         freq, duration, p2p->pending_listen_freq);
2964                 return;
2965         }
2966
2967         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2968                 "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
2969                 "callback",
2970                 p2p->pending_listen_sec, p2p->pending_listen_usec,
2971                 p2p->pending_listen_freq);
2972         p2p->in_listen = 1;
2973         p2p->drv_in_listen = freq;
2974         if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
2975                 /*
2976                  * Add 20 msec extra wait to avoid race condition with driver
2977                  * remain-on-channel end event, i.e., give driver more time to
2978                  * complete the operation before our timeout expires.
2979                  */
2980                 p2p_set_timeout(p2p, p2p->pending_listen_sec,
2981                                 p2p->pending_listen_usec + 20000);
2982         }
2983
2984         p2p->pending_listen_freq = 0;
2985 }
2986
2987
2988 int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
2989 {
2990         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
2991                 "state (freq=%u)", freq);
2992         p2p->drv_in_listen = 0;
2993         if (p2p->in_listen)
2994                 return 0; /* Internal timeout will trigger the next step */
2995
2996         if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
2997                 if (p2p->go_neg_peer->connect_reqs >= 120) {
2998                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2999                                 "P2P: Timeout on sending GO Negotiation "
3000                                 "Request without getting response");
3001                         p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3002                         return 0;
3003                 }
3004
3005                 p2p_set_state(p2p, P2P_CONNECT);
3006                 p2p_connect_send(p2p, p2p->go_neg_peer);
3007                 return 1;
3008         } else if (p2p->state == P2P_SEARCH) {
3009                 if (p2p->p2p_scan_running) {
3010                          /*
3011                           * Search is already in progress. This can happen if
3012                           * an Action frame RX is reported immediately after
3013                           * the end of a remain-on-channel operation and the
3014                           * response frame to that is sent using an offchannel
3015                           * operation while in p2p_find. Avoid an attempt to
3016                           * restart a scan here.
3017                           */
3018                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan "
3019                                 "already in progress - do not try to start a "
3020                                 "new one");
3021                         return 1;
3022                 }
3023                 if (p2p->pending_listen_freq) {
3024                         /*
3025                          * Better wait a bit if the driver is unable to start
3026                          * offchannel operation for some reason. p2p_search()
3027                          * will be started from internal timeout.
3028                          */
3029                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Listen "
3030                                 "operation did not seem to start - delay "
3031                                 "search phase to avoid busy loop");
3032                         p2p_set_timeout(p2p, 0, 100000);
3033                         return 1;
3034                 }
3035                 if (p2p->search_delay) {
3036                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
3037                                 "search operation by %u ms",
3038                                 p2p->search_delay);
3039                         p2p_set_timeout(p2p, p2p->search_delay / 1000,
3040                                         (p2p->search_delay % 1000) * 1000);
3041                         return 1;
3042                 }
3043                 p2p_search(p2p);
3044                 return 1;
3045         }
3046
3047         return 0;
3048 }
3049
3050
3051 static void p2p_timeout_connect(struct p2p_data *p2p)
3052 {
3053         p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3054         if (p2p->go_neg_peer &&
3055             (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
3056                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Wait for GO "
3057                         "Negotiation Confirm timed out - assume GO "
3058                         "Negotiation failed");
3059                 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3060                 return;
3061         }
3062         p2p_set_state(p2p, P2P_CONNECT_LISTEN);
3063         p2p_listen_in_find(p2p);
3064 }
3065
3066
3067 static void p2p_timeout_connect_listen(struct p2p_data *p2p)
3068 {
3069         if (p2p->go_neg_peer) {
3070                 if (p2p->drv_in_listen) {
3071                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
3072                                 "still in Listen state; wait for it to "
3073                                 "complete");
3074                         return;
3075                 }
3076
3077                 if (p2p->go_neg_peer->connect_reqs >= 120) {
3078                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3079                                 "P2P: Timeout on sending GO Negotiation "
3080                                 "Request without getting response");
3081                         p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3082                         return;
3083                 }
3084
3085                 p2p_set_state(p2p, P2P_CONNECT);
3086                 p2p_connect_send(p2p, p2p->go_neg_peer);
3087         } else
3088                 p2p_set_state(p2p, P2P_IDLE);
3089 }
3090
3091
3092 static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
3093 {
3094         /*
3095          * TODO: could remain constantly in Listen state for some time if there
3096          * are no other concurrent uses for the radio. For now, go to listen
3097          * state once per second to give other uses a chance to use the radio.
3098          */
3099         p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
3100         p2p_set_timeout(p2p, 0, 500000);
3101 }
3102
3103
3104 static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
3105 {
3106         struct p2p_device *dev = p2p->go_neg_peer;
3107
3108         if (dev == NULL) {
3109                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3110                         "P2P: Unknown GO Neg peer - stop GO Neg wait");
3111                 return;
3112         }
3113
3114         dev->wait_count++;
3115         if (dev->wait_count >= 120) {
3116                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3117                         "P2P: Timeout on waiting peer to become ready for GO "
3118                         "Negotiation");
3119                 p2p_go_neg_failed(p2p, dev, -1);
3120                 return;
3121         }
3122
3123         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3124                 "P2P: Go to Listen state while waiting for the peer to become "
3125                 "ready for GO Negotiation");
3126         p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
3127         p2p_listen_in_find(p2p);
3128 }
3129
3130
3131 static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
3132 {
3133         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3134                 "P2P: Service Discovery Query timeout");
3135         if (p2p->sd_peer) {
3136                 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3137                 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
3138                 p2p->sd_peer = NULL;
3139         }
3140         p2p_continue_find(p2p);
3141 }
3142
3143
3144 static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
3145 {
3146         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3147                 "P2P: Provision Discovery Request timeout");
3148         p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3149         p2p_continue_find(p2p);
3150 }
3151
3152
3153 static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
3154 {
3155         p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3156
3157         /*
3158          * For user initiated PD requests that we have not gotten any responses
3159          * for while in IDLE state, we retry them a couple of times before
3160          * giving up.
3161          */
3162         if (!p2p->user_initiated_pd)
3163                 return;
3164
3165         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3166                 "P2P: User initiated Provision Discovery Request timeout");
3167
3168         if (p2p->pd_retries) {
3169                 p2p->pd_retries--;
3170                 p2p_retry_pd(p2p);
3171         } else {
3172                 if (p2p->cfg->prov_disc_fail)
3173                         p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
3174                                                  p2p->pending_pd_devaddr,
3175                                                  P2P_PROV_DISC_TIMEOUT);
3176                 p2p_reset_pending_pd(p2p);
3177         }
3178 }
3179
3180
3181 static void p2p_timeout_invite(struct p2p_data *p2p)
3182 {
3183         p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3184         p2p_set_state(p2p, P2P_INVITE_LISTEN);
3185         if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
3186                 /*
3187                  * Better remain on operating channel instead of listen channel
3188                  * when running a group.
3189                  */
3190                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
3191                         "active GO role - wait on operating channel");
3192                 p2p_set_timeout(p2p, 0, 100000);
3193                 return;
3194         }
3195         p2p_listen_in_find(p2p);
3196 }
3197
3198
3199 static void p2p_timeout_invite_listen(struct p2p_data *p2p)
3200 {
3201         if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
3202                 p2p_set_state(p2p, P2P_INVITE);
3203                 p2p_invite_send(p2p, p2p->invite_peer,
3204                                 p2p->invite_go_dev_addr);
3205         } else {
3206                 if (p2p->invite_peer) {
3207                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3208                                 "P2P: Invitation Request retry limit reached");
3209                         if (p2p->cfg->invitation_result)
3210                                 p2p->cfg->invitation_result(
3211                                         p2p->cfg->cb_ctx, -1, NULL);
3212                 }
3213                 p2p_set_state(p2p, P2P_IDLE);
3214         }
3215 }
3216
3217
3218 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
3219 {
3220         struct p2p_data *p2p = eloop_ctx;
3221
3222         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
3223                 p2p_state_txt(p2p->state));
3224
3225         p2p->in_listen = 0;
3226
3227         switch (p2p->state) {
3228         case P2P_IDLE:
3229                 /* Check if we timed out waiting for PD req */
3230                 if (p2p->pending_action_state == P2P_PENDING_PD)
3231                         p2p_timeout_prov_disc_req(p2p);
3232                 break;
3233         case P2P_SEARCH:
3234                 /* Check if we timed out waiting for PD req */
3235                 if (p2p->pending_action_state == P2P_PENDING_PD)
3236                         p2p_timeout_prov_disc_req(p2p);
3237                 if (p2p->search_delay && !p2p->in_search_delay) {
3238                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
3239                                 "search operation by %u ms",
3240                                 p2p->search_delay);
3241                         p2p->in_search_delay = 1;
3242                         p2p_set_timeout(p2p, p2p->search_delay / 1000,
3243                                         (p2p->search_delay % 1000) * 1000);
3244                         break;
3245                 }
3246                 p2p->in_search_delay = 0;
3247                 p2p_search(p2p);
3248                 break;
3249         case P2P_CONNECT:
3250                 p2p_timeout_connect(p2p);
3251                 break;
3252         case P2P_CONNECT_LISTEN:
3253                 p2p_timeout_connect_listen(p2p);
3254                 break;
3255         case P2P_GO_NEG:
3256                 break;
3257         case P2P_LISTEN_ONLY:
3258                 /* Check if we timed out waiting for PD req */
3259                 if (p2p->pending_action_state == P2P_PENDING_PD)
3260                         p2p_timeout_prov_disc_req(p2p);
3261
3262                 if (p2p->ext_listen_only) {
3263                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3264                                 "P2P: Extended Listen Timing - Listen State "
3265                                 "completed");
3266                         p2p->ext_listen_only = 0;
3267                         p2p_set_state(p2p, P2P_IDLE);
3268                 }
3269                 break;
3270         case P2P_WAIT_PEER_CONNECT:
3271                 p2p_timeout_wait_peer_connect(p2p);
3272                 break;
3273         case P2P_WAIT_PEER_IDLE:
3274                 p2p_timeout_wait_peer_idle(p2p);
3275                 break;
3276         case P2P_SD_DURING_FIND:
3277                 p2p_timeout_sd_during_find(p2p);
3278                 break;
3279         case P2P_PROVISIONING:
3280                 break;
3281         case P2P_PD_DURING_FIND:
3282                 p2p_timeout_prov_disc_during_find(p2p);
3283                 break;
3284         case P2P_INVITE:
3285                 p2p_timeout_invite(p2p);
3286                 break;
3287         case P2P_INVITE_LISTEN:
3288                 p2p_timeout_invite_listen(p2p);
3289                 break;
3290         case P2P_SEARCH_WHEN_READY:
3291                 break;
3292         case P2P_CONTINUE_SEARCH_WHEN_READY:
3293                 break;
3294         }
3295 }
3296
3297
3298 int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
3299 {
3300         struct p2p_device *dev;
3301
3302         dev = p2p_get_device(p2p, peer_addr);
3303         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
3304                 "connection attempts by peer " MACSTR, MAC2STR(peer_addr));
3305         if (dev == NULL) {
3306                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
3307                         " unknown", MAC2STR(peer_addr));
3308                 return -1;
3309         }
3310         dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
3311         dev->flags |= P2P_DEV_USER_REJECTED;
3312         return 0;
3313 }
3314
3315
3316 const char * p2p_wps_method_text(enum p2p_wps_method method)
3317 {
3318         switch (method) {
3319         case WPS_NOT_READY:
3320                 return "not-ready";
3321         case WPS_PIN_DISPLAY:
3322                 return "Display";
3323         case WPS_PIN_KEYPAD:
3324                 return "Keypad";
3325         case WPS_PBC:
3326                 return "PBC";
3327         }
3328
3329         return "??";
3330 }
3331
3332
3333 static const char * p2p_go_state_text(enum p2p_go_state go_state)
3334 {
3335         switch (go_state) {
3336         case UNKNOWN_GO:
3337                 return "unknown";
3338         case LOCAL_GO:
3339                 return "local";
3340         case  REMOTE_GO:
3341                 return "remote";
3342         }
3343
3344         return "??";
3345 }
3346
3347
3348 const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
3349                                                const u8 *addr, int next)
3350 {
3351         struct p2p_device *dev;
3352
3353         if (addr)
3354                 dev = p2p_get_device(p2p, addr);
3355         else
3356                 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3357
3358         if (dev && next) {
3359                 dev = dl_list_first(&dev->list, struct p2p_device, list);
3360                 if (&dev->list == &p2p->devices)
3361                         dev = NULL;
3362         }
3363
3364         if (dev == NULL)
3365                 return NULL;
3366
3367         return &dev->info;
3368 }
3369
3370
3371 int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
3372                           char *buf, size_t buflen)
3373 {
3374         struct p2p_device *dev;
3375         int res;
3376         char *pos, *end;
3377         struct os_time now;
3378
3379         if (info == NULL)
3380                 return -1;
3381
3382         dev = (struct p2p_device *) (((u8 *) info) -
3383                                      offsetof(struct p2p_device, info));
3384
3385         pos = buf;
3386         end = buf + buflen;
3387
3388         os_get_time(&now);
3389         res = os_snprintf(pos, end - pos,
3390                           "age=%d\n"
3391                           "listen_freq=%d\n"
3392                           "wps_method=%s\n"
3393                           "interface_addr=" MACSTR "\n"
3394                           "member_in_go_dev=" MACSTR "\n"
3395                           "member_in_go_iface=" MACSTR "\n"
3396                           "go_neg_req_sent=%d\n"
3397                           "go_state=%s\n"
3398                           "dialog_token=%u\n"
3399                           "intended_addr=" MACSTR "\n"
3400                           "country=%c%c\n"
3401                           "oper_freq=%d\n"
3402                           "req_config_methods=0x%x\n"
3403                           "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
3404                           "status=%d\n"
3405                           "wait_count=%u\n"
3406                           "invitation_reqs=%u\n",
3407                           (int) (now.sec - dev->last_seen.sec),
3408                           dev->listen_freq,
3409                           p2p_wps_method_text(dev->wps_method),
3410                           MAC2STR(dev->interface_addr),
3411                           MAC2STR(dev->member_in_go_dev),
3412                           MAC2STR(dev->member_in_go_iface),
3413                           dev->go_neg_req_sent,
3414                           p2p_go_state_text(dev->go_state),
3415                           dev->dialog_token,
3416                           MAC2STR(dev->intended_addr),
3417                           dev->country[0] ? dev->country[0] : '_',
3418                           dev->country[1] ? dev->country[1] : '_',
3419                           dev->oper_freq,
3420                           dev->req_config_methods,
3421                           dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
3422                           "[PROBE_REQ_ONLY]" : "",
3423                           dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
3424                           dev->flags & P2P_DEV_NOT_YET_READY ?
3425                           "[NOT_YET_READY]" : "",
3426                           dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
3427                           dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
3428                           "",
3429                           dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
3430                           "[PD_PEER_DISPLAY]" : "",
3431                           dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
3432                           "[PD_PEER_KEYPAD]" : "",
3433                           dev->flags & P2P_DEV_USER_REJECTED ?
3434                           "[USER_REJECTED]" : "",
3435                           dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
3436                           "[PEER_WAITING_RESPONSE]" : "",
3437                           dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
3438                           "[PREFER_PERSISTENT_GROUP]" : "",
3439                           dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
3440                           "[WAIT_GO_NEG_RESPONSE]" : "",
3441                           dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
3442                           "[WAIT_GO_NEG_CONFIRM]" : "",
3443                           dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
3444                           "[GROUP_CLIENT_ONLY]" : "",
3445                           dev->flags & P2P_DEV_FORCE_FREQ ?
3446                           "[FORCE_FREQ]" : "",
3447                           dev->flags & P2P_DEV_PD_FOR_JOIN ?
3448                           "[PD_FOR_JOIN]" : "",
3449                           dev->status,
3450                           dev->wait_count,
3451                           dev->invitation_reqs);
3452         if (res < 0 || res >= end - pos)
3453                 return pos - buf;
3454         pos += res;
3455
3456         if (dev->ext_listen_period) {
3457                 res = os_snprintf(pos, end - pos,
3458                                   "ext_listen_period=%u\n"
3459                                   "ext_listen_interval=%u\n",
3460                                   dev->ext_listen_period,
3461                                   dev->ext_listen_interval);
3462                 if (res < 0 || res >= end - pos)
3463                         return pos - buf;
3464                 pos += res;
3465         }
3466
3467         if (dev->oper_ssid_len) {
3468                 res = os_snprintf(pos, end - pos,
3469                                   "oper_ssid=%s\n",
3470                                   wpa_ssid_txt(dev->oper_ssid,
3471                                                dev->oper_ssid_len));
3472                 if (res < 0 || res >= end - pos)
3473                         return pos - buf;
3474                 pos += res;
3475         }
3476
3477 #ifdef CONFIG_WIFI_DISPLAY
3478         if (dev->info.wfd_subelems) {
3479                 res = os_snprintf(pos, end - pos, "wfd_subelems=");
3480                 if (res < 0 || res >= end - pos)
3481                         return pos - buf;
3482                 pos += res;
3483
3484                 pos += wpa_snprintf_hex(pos, end - pos,
3485                                         wpabuf_head(dev->info.wfd_subelems),
3486                                         wpabuf_len(dev->info.wfd_subelems));
3487
3488                 res = os_snprintf(pos, end - pos, "\n");
3489                 if (res < 0 || res >= end - pos)
3490                         return pos - buf;
3491                 pos += res;
3492         }
3493 #endif /* CONFIG_WIFI_DISPLAY */
3494
3495         return pos - buf;
3496 }
3497
3498
3499 int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
3500 {
3501         return p2p_get_device(p2p, addr) != NULL;
3502 }
3503
3504
3505 void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
3506 {
3507         if (enabled) {
3508                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3509                         "discoverability enabled");
3510                 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3511         } else {
3512                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3513                         "discoverability disabled");
3514                 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3515         }
3516 }
3517
3518
3519 static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
3520                                               u32 duration2, u32 interval2)
3521 {
3522         struct wpabuf *req;
3523         struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
3524         u8 *len;
3525
3526         req = wpabuf_alloc(100);
3527         if (req == NULL)
3528                 return NULL;
3529
3530         if (duration1 || interval1) {
3531                 os_memset(&desc1, 0, sizeof(desc1));
3532                 desc1.count_type = 1;
3533                 desc1.duration = duration1;
3534                 desc1.interval = interval1;
3535                 ptr1 = &desc1;
3536
3537                 if (duration2 || interval2) {
3538                         os_memset(&desc2, 0, sizeof(desc2));
3539                         desc2.count_type = 2;
3540                         desc2.duration = duration2;
3541                         desc2.interval = interval2;
3542                         ptr2 = &desc2;
3543                 }
3544         }
3545
3546         p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
3547         len = p2p_buf_add_ie_hdr(req);
3548         p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
3549         p2p_buf_update_ie_hdr(req, len);
3550
3551         return req;
3552 }
3553
3554
3555 int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
3556                      const u8 *own_interface_addr, unsigned int freq,
3557                      u32 duration1, u32 interval1, u32 duration2,
3558                      u32 interval2)
3559 {
3560         struct wpabuf *req;
3561
3562         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
3563                 "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
3564                 "int1=%u dur2=%u int2=%u",
3565                 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
3566                 freq, duration1, interval1, duration2, interval2);
3567
3568         req = p2p_build_presence_req(duration1, interval1, duration2,
3569                                      interval2);
3570         if (req == NULL)
3571                 return -1;
3572
3573         p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3574         if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
3575                             go_interface_addr,
3576                             wpabuf_head(req), wpabuf_len(req), 200) < 0) {
3577                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3578                         "P2P: Failed to send Action frame");
3579         }
3580         wpabuf_free(req);
3581
3582         return 0;
3583 }
3584
3585
3586 static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
3587                                                size_t noa_len, u8 dialog_token)
3588 {
3589         struct wpabuf *resp;
3590         u8 *len;
3591
3592         resp = wpabuf_alloc(100 + noa_len);
3593         if (resp == NULL)
3594                 return NULL;
3595
3596         p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
3597         len = p2p_buf_add_ie_hdr(resp);
3598         p2p_buf_add_status(resp, status);
3599         if (noa) {
3600                 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
3601                 wpabuf_put_le16(resp, noa_len);
3602                 wpabuf_put_data(resp, noa, noa_len);
3603         } else
3604                 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
3605         p2p_buf_update_ie_hdr(resp, len);
3606
3607         return resp;
3608 }
3609
3610
3611 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
3612                                      const u8 *sa, const u8 *data, size_t len,
3613                                      int rx_freq)
3614 {
3615         struct p2p_message msg;
3616         u8 status;
3617         struct wpabuf *resp;
3618         size_t g;
3619         struct p2p_group *group = NULL;
3620         int parsed = 0;
3621         u8 noa[50];
3622         int noa_len;
3623
3624         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3625                 "P2P: Received P2P Action - P2P Presence Request");
3626
3627         for (g = 0; g < p2p->num_groups; g++) {
3628                 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
3629                               ETH_ALEN) == 0) {
3630                         group = p2p->groups[g];
3631                         break;
3632                 }
3633         }
3634         if (group == NULL) {
3635                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3636                         "P2P: Ignore P2P Presence Request for unknown group "
3637                         MACSTR, MAC2STR(da));
3638                 return;
3639         }
3640
3641         if (p2p_parse(data, len, &msg) < 0) {
3642                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3643                         "P2P: Failed to parse P2P Presence Request");
3644                 status = P2P_SC_FAIL_INVALID_PARAMS;
3645                 goto fail;
3646         }
3647         parsed = 1;
3648
3649         if (msg.noa == NULL) {
3650                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3651                         "P2P: No NoA attribute in P2P Presence Request");
3652                 status = P2P_SC_FAIL_INVALID_PARAMS;
3653                 goto fail;
3654         }
3655
3656         status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
3657
3658 fail:
3659         if (p2p->cfg->get_noa)
3660                 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
3661                                             sizeof(noa));
3662         else
3663                 noa_len = -1;
3664         resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
3665                                        noa_len > 0 ? noa_len : 0,
3666                                        msg.dialog_token);
3667         if (parsed)
3668                 p2p_parse_free(&msg);
3669         if (resp == NULL)
3670                 return;
3671
3672         p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3673         if (p2p_send_action(p2p, rx_freq, sa, da, da,
3674                             wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
3675                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3676                         "P2P: Failed to send Action frame");
3677         }
3678         wpabuf_free(resp);
3679 }
3680
3681
3682 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
3683                                       const u8 *sa, const u8 *data, size_t len)
3684 {
3685         struct p2p_message msg;
3686
3687         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3688                 "P2P: Received P2P Action - P2P Presence Response");
3689
3690         if (p2p_parse(data, len, &msg) < 0) {
3691                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3692                         "P2P: Failed to parse P2P Presence Response");
3693                 return;
3694         }
3695
3696         if (msg.status == NULL || msg.noa == NULL) {
3697                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3698                         "P2P: No Status or NoA attribute in P2P Presence "
3699                         "Response");
3700                 p2p_parse_free(&msg);
3701                 return;
3702         }
3703
3704         if (*msg.status) {
3705                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3706                         "P2P: P2P Presence Request was rejected: status %u",
3707                         *msg.status);
3708                 p2p_parse_free(&msg);
3709                 return;
3710         }
3711
3712         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3713                 "P2P: P2P Presence Request was accepted");
3714         wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
3715                     msg.noa, msg.noa_len);
3716         /* TODO: process NoA */
3717         p2p_parse_free(&msg);
3718 }
3719
3720
3721 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3722 {
3723         struct p2p_data *p2p = eloop_ctx;
3724
3725         if (p2p->ext_listen_interval) {
3726                 /* Schedule next extended listen timeout */
3727                 eloop_register_timeout(p2p->ext_listen_interval_sec,
3728                                        p2p->ext_listen_interval_usec,
3729                                        p2p_ext_listen_timeout, p2p, NULL);
3730         }
3731
3732         if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
3733                 /*
3734                  * This should not really happen, but it looks like the Listen
3735                  * command may fail is something else (e.g., a scan) was
3736                  * running at an inconvenient time. As a workaround, allow new
3737                  * Extended Listen operation to be started.
3738                  */
3739                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
3740                         "Extended Listen operation had not been completed - "
3741                         "try again");
3742                 p2p->ext_listen_only = 0;
3743                 p2p_set_state(p2p, P2P_IDLE);
3744         }
3745
3746         if (p2p->state != P2P_IDLE) {
3747                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
3748                         "Listen timeout in active state (%s)",
3749                         p2p_state_txt(p2p->state));
3750                 return;
3751         }
3752
3753         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
3754         p2p->ext_listen_only = 1;
3755         if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
3756                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
3757                         "Listen state for Extended Listen Timing");
3758                 p2p->ext_listen_only = 0;
3759         }
3760 }
3761
3762
3763 int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
3764                    unsigned int interval)
3765 {
3766         if (period > 65535 || interval > 65535 || period > interval ||
3767             (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
3768                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3769                         "P2P: Invalid Extended Listen Timing request: "
3770                         "period=%u interval=%u", period, interval);
3771                 return -1;
3772         }
3773
3774         eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
3775
3776         if (interval == 0) {
3777                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3778                         "P2P: Disabling Extended Listen Timing");
3779                 p2p->ext_listen_period = 0;
3780                 p2p->ext_listen_interval = 0;
3781                 return 0;
3782         }
3783
3784         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3785                 "P2P: Enabling Extended Listen Timing: period %u msec, "
3786                 "interval %u msec", period, interval);
3787         p2p->ext_listen_period = period;
3788         p2p->ext_listen_interval = interval;
3789         p2p->ext_listen_interval_sec = interval / 1000;
3790         p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
3791
3792         eloop_register_timeout(p2p->ext_listen_interval_sec,
3793                                p2p->ext_listen_interval_usec,
3794                                p2p_ext_listen_timeout, p2p, NULL);
3795
3796         return 0;
3797 }
3798
3799
3800 void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3801                       const u8 *ie, size_t ie_len)
3802 {
3803         struct p2p_message msg;
3804
3805         if (bssid == NULL || ie == NULL)
3806                 return;
3807
3808         os_memset(&msg, 0, sizeof(msg));
3809         if (p2p_parse_ies(ie, ie_len, &msg))
3810                 return;
3811         if (msg.minor_reason_code == NULL)
3812                 return;
3813
3814         wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3815                 "P2P: Deauthentication notification BSSID " MACSTR
3816                 " reason_code=%u minor_reason_code=%u",
3817                 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3818
3819         p2p_parse_free(&msg);
3820 }
3821
3822
3823 void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3824                         const u8 *ie, size_t ie_len)
3825 {
3826         struct p2p_message msg;
3827
3828         if (bssid == NULL || ie == NULL)
3829                 return;
3830
3831         os_memset(&msg, 0, sizeof(msg));
3832         if (p2p_parse_ies(ie, ie_len, &msg))
3833                 return;
3834         if (msg.minor_reason_code == NULL)
3835                 return;
3836
3837         wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3838                 "P2P: Disassociation notification BSSID " MACSTR
3839                 " reason_code=%u minor_reason_code=%u",
3840                 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3841
3842         p2p_parse_free(&msg);
3843 }
3844
3845
3846 void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
3847 {
3848         if (enabled) {
3849                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3850                         "Device operations enabled");
3851                 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
3852         } else {
3853                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3854                         "Device operations disabled");
3855                 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
3856         }
3857 }
3858
3859
3860 int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
3861 {
3862         if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
3863                 return -1;
3864
3865         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
3866                 "reg_class %u channel %u", reg_class, channel);
3867         p2p->cfg->reg_class = reg_class;
3868         p2p->cfg->channel = channel;
3869
3870         return 0;
3871 }
3872
3873
3874 int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
3875 {
3876         wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
3877         if (postfix == NULL) {
3878                 p2p->cfg->ssid_postfix_len = 0;
3879                 return 0;
3880         }
3881         if (len > sizeof(p2p->cfg->ssid_postfix))
3882                 return -1;
3883         os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
3884         p2p->cfg->ssid_postfix_len = len;
3885         return 0;
3886 }
3887
3888
3889 int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
3890                          int cfg_op_channel)
3891 {
3892         if (p2p_channel_to_freq(p2p->cfg->country, op_reg_class, op_channel)
3893             < 0)
3894                 return -1;
3895
3896         wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, "P2P: Set Operating channel: "
3897                 "reg_class %u channel %u", op_reg_class, op_channel);
3898         p2p->cfg->op_reg_class = op_reg_class;
3899         p2p->cfg->op_channel = op_channel;
3900         p2p->cfg->cfg_op_channel = cfg_op_channel;
3901         return 0;
3902 }
3903
3904
3905 int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
3906                       const struct p2p_channel *pref_chan)
3907 {
3908         struct p2p_channel *n;
3909
3910         if (pref_chan) {
3911                 n = os_malloc(num_pref_chan * sizeof(struct p2p_channel));
3912                 if (n == NULL)
3913                         return -1;
3914                 os_memcpy(n, pref_chan,
3915                           num_pref_chan * sizeof(struct p2p_channel));
3916         } else
3917                 n = NULL;
3918
3919         os_free(p2p->cfg->pref_chan);
3920         p2p->cfg->pref_chan = n;
3921         p2p->cfg->num_pref_chan = num_pref_chan;
3922
3923         return 0;
3924 }
3925
3926
3927 int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
3928                            u8 *iface_addr)
3929 {
3930         struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
3931         if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
3932                 return -1;
3933         os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
3934         return 0;
3935 }
3936
3937
3938 int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
3939                            u8 *dev_addr)
3940 {
3941         struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3942         if (dev == NULL)
3943                 return -1;
3944         os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
3945         return 0;
3946 }
3947
3948
3949 void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
3950 {
3951         os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
3952         if (is_zero_ether_addr(p2p->peer_filter))
3953                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
3954                         "filter");
3955         else
3956                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
3957                         "filter for " MACSTR, MAC2STR(p2p->peer_filter));
3958 }
3959
3960
3961 void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
3962 {
3963         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
3964                 enabled ? "enabled" : "disabled");
3965         if (p2p->cross_connect == enabled)
3966                 return;
3967         p2p->cross_connect = enabled;
3968         /* TODO: may need to tear down any action group where we are GO(?) */
3969 }
3970
3971
3972 int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
3973 {
3974         struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3975         if (dev == NULL)
3976                 return -1;
3977         if (dev->oper_freq <= 0)
3978                 return -1;
3979         return dev->oper_freq;
3980 }
3981
3982
3983 void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
3984 {
3985         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s",
3986                 enabled ? "enabled" : "disabled");
3987         p2p->cfg->p2p_intra_bss = enabled;
3988 }
3989
3990
3991 void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan)
3992 {
3993         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list");
3994         os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
3995 }
3996
3997
3998 int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3999                     const u8 *src, const u8 *bssid, const u8 *buf,
4000                     size_t len, unsigned int wait_time)
4001 {
4002         if (p2p->p2p_scan_running) {
4003                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action "
4004                         "frame TX until p2p_scan completes");
4005                 if (p2p->after_scan_tx) {
4006                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
4007                                 "previous pending Action frame TX");
4008                         os_free(p2p->after_scan_tx);
4009                 }
4010                 p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
4011                                                len);
4012                 if (p2p->after_scan_tx == NULL)
4013                         return -1;
4014                 p2p->after_scan_tx->freq = freq;
4015                 os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
4016                 os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
4017                 os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
4018                 p2p->after_scan_tx->len = len;
4019                 p2p->after_scan_tx->wait_time = wait_time;
4020                 os_memcpy(p2p->after_scan_tx + 1, buf, len);
4021                 return 0;
4022         }
4023
4024         return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
4025                                      buf, len, wait_time);
4026 }
4027
4028
4029 void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
4030                            int freq_overall)
4031 {
4032         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d,"
4033                 "  5 GHz: %d,  overall: %d", freq_24, freq_5, freq_overall);
4034         p2p->best_freq_24 = freq_24;
4035         p2p->best_freq_5 = freq_5;
4036         p2p->best_freq_overall = freq_overall;
4037 }
4038
4039
4040 const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
4041 {
4042         if (p2p == NULL || p2p->go_neg_peer == NULL)
4043                 return NULL;
4044         return p2p->go_neg_peer->info.p2p_device_addr;
4045 }
4046
4047
4048 const struct p2p_peer_info *
4049 p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
4050 {
4051         struct p2p_device *dev;
4052
4053         if (addr) {
4054                 dev = p2p_get_device(p2p, addr);
4055                 if (!dev)
4056                         return NULL;
4057
4058                 if (!next) {
4059                         if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
4060                                 return NULL;
4061
4062                         return &dev->info;
4063                 } else {
4064                         do {
4065                                 dev = dl_list_first(&dev->list,
4066                                                     struct p2p_device,
4067                                                     list);
4068                                 if (&dev->list == &p2p->devices)
4069                                         return NULL;
4070                         } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
4071                 }
4072         } else {
4073                 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4074                 if (!dev)
4075                         return NULL;
4076                 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
4077                         dev = dl_list_first(&dev->list,
4078                                             struct p2p_device,
4079                                             list);
4080                         if (&dev->list == &p2p->devices)
4081                                 return NULL;
4082                 }
4083         }
4084
4085         return &dev->info;
4086 }
4087
4088
4089 int p2p_in_progress(struct p2p_data *p2p)
4090 {
4091         if (p2p == NULL)
4092                 return 0;
4093         if (p2p->state == P2P_SEARCH || p2p->state == P2P_SEARCH_WHEN_READY ||
4094             p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY)
4095                 return 2;
4096         return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
4097 }
4098
4099
4100 void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
4101                             u8 client_timeout)
4102 {
4103         if (p2p) {
4104                 p2p->go_timeout = go_timeout;
4105                 p2p->client_timeout = client_timeout;
4106         }
4107 }
4108
4109
4110 void p2p_increase_search_delay(struct p2p_data *p2p, unsigned int delay)
4111 {
4112         if (p2p && p2p->search_delay < delay)
4113                 p2p->search_delay = delay;
4114 }
4115
4116
4117 #ifdef CONFIG_WIFI_DISPLAY
4118
4119 static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
4120 {
4121         size_t g;
4122         struct p2p_group *group;
4123
4124         for (g = 0; g < p2p->num_groups; g++) {
4125                 group = p2p->groups[g];
4126                 p2p_group_update_ies(group);
4127         }
4128 }
4129
4130
4131 int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
4132 {
4133         wpabuf_free(p2p->wfd_ie_beacon);
4134         p2p->wfd_ie_beacon = ie;
4135         p2p_update_wfd_ie_groups(p2p);
4136         return 0;
4137 }
4138
4139
4140 int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
4141 {
4142         wpabuf_free(p2p->wfd_ie_probe_req);
4143         p2p->wfd_ie_probe_req = ie;
4144         return 0;
4145 }
4146
4147
4148 int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
4149 {
4150         wpabuf_free(p2p->wfd_ie_probe_resp);
4151         p2p->wfd_ie_probe_resp = ie;
4152         p2p_update_wfd_ie_groups(p2p);
4153         return 0;
4154 }
4155
4156
4157 int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
4158 {
4159         wpabuf_free(p2p->wfd_ie_assoc_req);
4160         p2p->wfd_ie_assoc_req = ie;
4161         return 0;
4162 }
4163
4164
4165 int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
4166 {
4167         wpabuf_free(p2p->wfd_ie_invitation);
4168         p2p->wfd_ie_invitation = ie;
4169         return 0;
4170 }
4171
4172
4173 int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
4174 {
4175         wpabuf_free(p2p->wfd_ie_prov_disc_req);
4176         p2p->wfd_ie_prov_disc_req = ie;
4177         return 0;
4178 }
4179
4180
4181 int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
4182 {
4183         wpabuf_free(p2p->wfd_ie_prov_disc_resp);
4184         p2p->wfd_ie_prov_disc_resp = ie;
4185         return 0;
4186 }
4187
4188
4189 int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
4190 {
4191         wpabuf_free(p2p->wfd_ie_go_neg);
4192         p2p->wfd_ie_go_neg = ie;
4193         return 0;
4194 }
4195
4196
4197 int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
4198 {
4199         wpabuf_free(p2p->wfd_dev_info);
4200         if (elem) {
4201                 p2p->wfd_dev_info = wpabuf_dup(elem);
4202                 if (p2p->wfd_dev_info == NULL)
4203                         return -1;
4204         } else
4205                 p2p->wfd_dev_info = NULL;
4206
4207         return 0;
4208 }
4209
4210
4211 int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
4212 {
4213         wpabuf_free(p2p->wfd_assoc_bssid);
4214         if (elem) {
4215                 p2p->wfd_assoc_bssid = wpabuf_dup(elem);
4216                 if (p2p->wfd_assoc_bssid == NULL)
4217                         return -1;
4218         } else
4219                 p2p->wfd_assoc_bssid = NULL;
4220
4221         return 0;
4222 }
4223
4224
4225 int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
4226                                   const struct wpabuf *elem)
4227 {
4228         wpabuf_free(p2p->wfd_coupled_sink_info);
4229         if (elem) {
4230                 p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
4231                 if (p2p->wfd_coupled_sink_info == NULL)
4232                         return -1;
4233         } else
4234                 p2p->wfd_coupled_sink_info = NULL;
4235
4236         return 0;
4237 }
4238
4239 #endif /* CONFIG_WIFI_DISPLAY */