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