P2P: Enable 40 MHz support for autonomous P2P group addition
[mech_eap.git] / wpa_supplicant / p2p_supplicant.c
1 /*
2  * wpa_supplicant - P2P
3  * Copyright (c) 2009-2010, Atheros Communications
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "eloop.h"
13 #include "common/ieee802_11_common.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/wpa_ctrl.h"
16 #include "wps/wps_i.h"
17 #include "p2p/p2p.h"
18 #include "ap/hostapd.h"
19 #include "ap/ap_config.h"
20 #include "ap/p2p_hostapd.h"
21 #include "eapol_supp/eapol_supp_sm.h"
22 #include "rsn_supp/wpa.h"
23 #include "wpa_supplicant_i.h"
24 #include "driver_i.h"
25 #include "ap.h"
26 #include "config_ssid.h"
27 #include "config.h"
28 #include "notify.h"
29 #include "scan.h"
30 #include "bss.h"
31 #include "offchannel.h"
32 #include "wps_supplicant.h"
33 #include "p2p_supplicant.h"
34
35
36 /*
37  * How many times to try to scan to find the GO before giving up on join
38  * request.
39  */
40 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
41
42 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
43
44 #ifndef P2P_MAX_CLIENT_IDLE
45 /*
46  * How many seconds to try to reconnect to the GO when connection in P2P client
47  * role has been lost.
48  */
49 #define P2P_MAX_CLIENT_IDLE 10
50 #endif /* P2P_MAX_CLIENT_IDLE */
51
52 #ifndef P2P_MAX_INITIAL_CONN_WAIT
53 /*
54  * How many seconds to wait for initial 4-way handshake to get completed after
55  * WPS provisioning step.
56  */
57 #define P2P_MAX_INITIAL_CONN_WAIT 10
58 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
59
60 enum p2p_group_removal_reason {
61         P2P_GROUP_REMOVAL_UNKNOWN,
62         P2P_GROUP_REMOVAL_SILENT,
63         P2P_GROUP_REMOVAL_FORMATION_FAILED,
64         P2P_GROUP_REMOVAL_REQUESTED,
65         P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
66         P2P_GROUP_REMOVAL_UNAVAILABLE,
67         P2P_GROUP_REMOVAL_GO_ENDING_SESSION
68 };
69
70
71 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
72 static struct wpa_supplicant *
73 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
74                          int go);
75 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
76 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
77 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
78 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
79                          const u8 *dev_addr, enum p2p_wps_method wps_method,
80                          int auto_join);
81 static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx,
82                                             void *timeout_ctx);
83 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
84 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
85 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
86 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
87 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
88                                         int group_added);
89
90
91 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
92                                       struct wpa_scan_results *scan_res)
93 {
94         size_t i;
95
96         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
97                 return;
98
99         wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
100                    (int) scan_res->num);
101
102         for (i = 0; i < scan_res->num; i++) {
103                 struct wpa_scan_res *bss = scan_res->res[i];
104                 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
105                                          bss->freq, bss->level,
106                                          (const u8 *) (bss + 1),
107                                          bss->ie_len) > 0)
108                         break;
109         }
110
111         p2p_scan_res_handled(wpa_s->global->p2p);
112 }
113
114
115 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
116                          unsigned int num_req_dev_types,
117                          const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
118 {
119         struct wpa_supplicant *wpa_s = ctx;
120         struct wpa_driver_scan_params params;
121         int ret;
122         struct wpabuf *wps_ie, *ies;
123         int social_channels[] = { 2412, 2437, 2462, 0, 0 };
124         size_t ielen;
125
126         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
127                 return -1;
128
129         os_memset(&params, 0, sizeof(params));
130
131         /* P2P Wildcard SSID */
132         params.num_ssids = 1;
133         params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
134         params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
135
136         wpa_s->wps->dev.p2p = 1;
137         wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
138                                         wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
139                                         num_req_dev_types, req_dev_types);
140         if (wps_ie == NULL)
141                 return -1;
142
143         ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
144         ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
145         if (ies == NULL) {
146                 wpabuf_free(wps_ie);
147                 return -1;
148         }
149         wpabuf_put_buf(ies, wps_ie);
150         wpabuf_free(wps_ie);
151
152         p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
153
154         params.p2p_probe = 1;
155         params.extra_ies = wpabuf_head(ies);
156         params.extra_ies_len = wpabuf_len(ies);
157
158         switch (type) {
159         case P2P_SCAN_SOCIAL:
160                 params.freqs = social_channels;
161                 break;
162         case P2P_SCAN_FULL:
163                 break;
164         case P2P_SCAN_SOCIAL_PLUS_ONE:
165                 social_channels[3] = freq;
166                 params.freqs = social_channels;
167                 break;
168         }
169
170         ret = wpa_drv_scan(wpa_s, &params);
171
172         wpabuf_free(ies);
173
174         if (ret) {
175                 if (wpa_s->scanning ||
176                     wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
177                         wpa_s->p2p_cb_on_scan_complete = 1;
178                         ret = 1;
179                 }
180         } else
181                 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
182
183         return ret;
184 }
185
186
187 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
188 {
189         switch (p2p_group_interface) {
190         case P2P_GROUP_INTERFACE_PENDING:
191                 return WPA_IF_P2P_GROUP;
192         case P2P_GROUP_INTERFACE_GO:
193                 return WPA_IF_P2P_GO;
194         case P2P_GROUP_INTERFACE_CLIENT:
195                 return WPA_IF_P2P_CLIENT;
196         }
197
198         return WPA_IF_P2P_GROUP;
199 }
200
201
202 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
203                                                   const u8 *ssid,
204                                                   size_t ssid_len, int *go)
205 {
206         struct wpa_ssid *s;
207
208         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
209                 for (s = wpa_s->conf->ssid; s; s = s->next) {
210                         if (s->disabled != 0 || !s->p2p_group ||
211                             s->ssid_len != ssid_len ||
212                             os_memcmp(ssid, s->ssid, ssid_len) != 0)
213                                 continue;
214                         if (s->mode == WPAS_MODE_P2P_GO &&
215                             s != wpa_s->current_ssid)
216                                 continue;
217                         if (go)
218                                 *go = s->mode == WPAS_MODE_P2P_GO;
219                         return wpa_s;
220                 }
221         }
222
223         return NULL;
224 }
225
226
227 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
228                                  enum p2p_group_removal_reason removal_reason)
229 {
230         struct wpa_ssid *ssid;
231         char *gtype;
232         const char *reason;
233
234         ssid = wpa_s->current_ssid;
235         if (ssid == NULL) {
236                 /*
237                  * The current SSID was not known, but there may still be a
238                  * pending P2P group interface waiting for provisioning.
239                  */
240                 ssid = wpa_s->conf->ssid;
241                 while (ssid) {
242                         if (ssid->p2p_group &&
243                             (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
244                              (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
245                                 break;
246                         ssid = ssid->next;
247                 }
248                 if (ssid == NULL) {
249                         wpa_printf(MSG_ERROR, "P2P: P2P group interface "
250                                    "not found");
251                         return -1;
252                 }
253         }
254         if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
255                 gtype = "GO";
256         else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
257                  (ssid && ssid->mode == WPAS_MODE_INFRA)) {
258                 wpa_s->reassociate = 0;
259                 wpa_s->disconnected = 1;
260                 wpa_supplicant_deauthenticate(wpa_s,
261                                               WLAN_REASON_DEAUTH_LEAVING);
262                 gtype = "client";
263         } else
264                 gtype = "GO";
265         if (wpa_s->cross_connect_in_use) {
266                 wpa_s->cross_connect_in_use = 0;
267                 wpa_msg(wpa_s->parent, MSG_INFO,
268                         P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
269                         wpa_s->ifname, wpa_s->cross_connect_uplink);
270         }
271         switch (removal_reason) {
272         case P2P_GROUP_REMOVAL_REQUESTED:
273                 reason = " reason=REQUESTED";
274                 break;
275         case P2P_GROUP_REMOVAL_FORMATION_FAILED:
276                 reason = " reason=FORMATION_FAILED";
277                 break;
278         case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
279                 reason = " reason=IDLE";
280                 break;
281         case P2P_GROUP_REMOVAL_UNAVAILABLE:
282                 reason = " reason=UNAVAILABLE";
283                 break;
284         case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
285                 reason = " reason=GO_ENDING_SESSION";
286                 break;
287         default:
288                 reason = "";
289                 break;
290         }
291         if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
292                 wpa_msg(wpa_s->parent, MSG_INFO,
293                         P2P_EVENT_GROUP_REMOVED "%s %s%s",
294                         wpa_s->ifname, gtype, reason);
295         }
296
297         if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
298                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
299
300         if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
301                 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
302
303         if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
304                 struct wpa_global *global;
305                 char *ifname;
306                 enum wpa_driver_if_type type;
307                 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
308                         wpa_s->ifname);
309                 global = wpa_s->global;
310                 ifname = os_strdup(wpa_s->ifname);
311                 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
312                 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
313                 wpa_s = global->ifaces;
314                 if (wpa_s && ifname)
315                         wpa_drv_if_remove(wpa_s, type, ifname);
316                 os_free(ifname);
317                 return 0;
318         }
319
320         wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
321         if (ssid && (ssid->p2p_group ||
322                      ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
323                      (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
324                 int id = ssid->id;
325                 if (ssid == wpa_s->current_ssid) {
326                         wpa_sm_set_config(wpa_s->wpa, NULL);
327                         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
328                         wpa_s->current_ssid = NULL;
329                 }
330                 /*
331                  * Networks objects created during any P2P activities are not
332                  * exposed out as they might/will confuse certain non-P2P aware
333                  * applications since these network objects won't behave like
334                  * regular ones.
335                  *
336                  * Likewise, we don't send out network removed signals for such
337                  * network objects.
338                  */
339                 wpa_config_remove_network(wpa_s->conf, id);
340                 wpa_supplicant_clear_status(wpa_s);
341                 wpa_supplicant_cancel_sched_scan(wpa_s);
342         } else {
343                 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
344                            "found");
345         }
346         if (wpa_s->ap_iface)
347                 wpa_supplicant_ap_deinit(wpa_s);
348         else
349                 wpa_drv_deinit_p2p_cli(wpa_s);
350
351         return 0;
352 }
353
354
355 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
356                                      u8 *go_dev_addr,
357                                      const u8 *ssid, size_t ssid_len)
358 {
359         struct wpa_bss *bss;
360         const u8 *bssid;
361         struct wpabuf *p2p;
362         u8 group_capab;
363         const u8 *addr;
364
365         if (wpa_s->go_params)
366                 bssid = wpa_s->go_params->peer_interface_addr;
367         else
368                 bssid = wpa_s->bssid;
369
370         bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
371         if (bss == NULL) {
372                 u8 iface_addr[ETH_ALEN];
373                 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
374                                            iface_addr) == 0)
375                         bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
376         }
377         if (bss == NULL) {
378                 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
379                            "group is persistent - BSS " MACSTR " not found",
380                            MAC2STR(bssid));
381                 return 0;
382         }
383
384         p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
385         if (p2p == NULL) {
386                 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
387                            "group is persistent - BSS " MACSTR
388                            " did not include P2P IE", MAC2STR(bssid));
389                 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
390                             (u8 *) (bss + 1), bss->ie_len);
391                 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
392                             ((u8 *) bss + 1) + bss->ie_len,
393                             bss->beacon_ie_len);
394                 return 0;
395         }
396
397         group_capab = p2p_get_group_capab(p2p);
398         addr = p2p_get_go_dev_addr(p2p);
399         wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
400                    "group_capab=0x%x", group_capab);
401         if (addr) {
402                 os_memcpy(go_dev_addr, addr, ETH_ALEN);
403                 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
404                            MAC2STR(addr));
405         } else
406                 os_memset(go_dev_addr, 0, ETH_ALEN);
407         wpabuf_free(p2p);
408
409         wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
410                    "go_dev_addr=" MACSTR,
411                    MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
412
413         return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
414 }
415
416
417 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
418                                            struct wpa_ssid *ssid,
419                                            const u8 *go_dev_addr)
420 {
421         struct wpa_ssid *s;
422         int changed = 0;
423
424         wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
425                    "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
426         for (s = wpa_s->conf->ssid; s; s = s->next) {
427                 if (s->disabled == 2 &&
428                     os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
429                     s->ssid_len == ssid->ssid_len &&
430                     os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
431                         break;
432         }
433
434         if (s) {
435                 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
436                            "entry");
437                 if (ssid->passphrase && !s->passphrase)
438                         changed = 1;
439                 else if (ssid->passphrase && s->passphrase &&
440                          os_strcmp(ssid->passphrase, s->passphrase) != 0)
441                         changed = 1;
442         } else {
443                 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
444                            "entry");
445                 changed = 1;
446                 s = wpa_config_add_network(wpa_s->conf);
447                 if (s == NULL)
448                         return -1;
449
450                 /*
451                  * Instead of network_added we emit persistent_group_added
452                  * notification. Also to keep the defense checks in
453                  * persistent_group obj registration method, we set the
454                  * relevant flags in s to designate it as a persistent group.
455                  */
456                 s->p2p_group = 1;
457                 s->p2p_persistent_group = 1;
458                 wpas_notify_persistent_group_added(wpa_s, s);
459                 wpa_config_set_network_defaults(s);
460         }
461
462         s->p2p_group = 1;
463         s->p2p_persistent_group = 1;
464         s->disabled = 2;
465         s->bssid_set = 1;
466         os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
467         s->mode = ssid->mode;
468         s->auth_alg = WPA_AUTH_ALG_OPEN;
469         s->key_mgmt = WPA_KEY_MGMT_PSK;
470         s->proto = WPA_PROTO_RSN;
471         s->pairwise_cipher = WPA_CIPHER_CCMP;
472         s->export_keys = 1;
473         if (ssid->passphrase) {
474                 os_free(s->passphrase);
475                 s->passphrase = os_strdup(ssid->passphrase);
476         }
477         if (ssid->psk_set) {
478                 s->psk_set = 1;
479                 os_memcpy(s->psk, ssid->psk, 32);
480         }
481         if (s->passphrase && !s->psk_set)
482                 wpa_config_update_psk(s);
483         if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
484                 os_free(s->ssid);
485                 s->ssid = os_malloc(ssid->ssid_len);
486         }
487         if (s->ssid) {
488                 s->ssid_len = ssid->ssid_len;
489                 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
490         }
491
492 #ifndef CONFIG_NO_CONFIG_WRITE
493         if (changed && wpa_s->conf->update_config &&
494             wpa_config_write(wpa_s->confname, wpa_s->conf)) {
495                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
496         }
497 #endif /* CONFIG_NO_CONFIG_WRITE */
498
499         return s->id;
500 }
501
502
503 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
504                                                  const u8 *addr)
505 {
506         struct wpa_ssid *ssid, *s;
507         u8 *n;
508         size_t i;
509
510         ssid = wpa_s->current_ssid;
511         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
512             !ssid->p2p_persistent_group)
513                 return;
514
515         for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
516                 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
517                         continue;
518
519                 if (s->ssid_len == ssid->ssid_len &&
520                     os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
521                         break;
522         }
523
524         if (s == NULL)
525                 return;
526
527         for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
528                 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
529                               ETH_ALEN) == 0)
530                         return; /* already in list */
531         }
532
533         n = os_realloc_array(s->p2p_client_list, s->num_p2p_clients + 1,
534                              ETH_ALEN);
535         if (n == NULL)
536                 return;
537         os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
538         s->p2p_client_list = n;
539         s->num_p2p_clients++;
540
541 #ifndef CONFIG_NO_CONFIG_WRITE
542         if (wpa_s->parent->conf->update_config &&
543             wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
544                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
545 #endif /* CONFIG_NO_CONFIG_WRITE */
546 }
547
548
549 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
550                                            int success)
551 {
552         struct wpa_ssid *ssid;
553         const char *ssid_txt;
554         int client;
555         int persistent;
556         u8 go_dev_addr[ETH_ALEN];
557         int network_id = -1;
558
559         /*
560          * This callback is likely called for the main interface. Update wpa_s
561          * to use the group interface if a new interface was created for the
562          * group.
563          */
564         if (wpa_s->global->p2p_group_formation)
565                 wpa_s = wpa_s->global->p2p_group_formation;
566         wpa_s->global->p2p_group_formation = NULL;
567         wpa_s->p2p_in_provisioning = 0;
568
569         if (!success) {
570                 wpa_msg(wpa_s->parent, MSG_INFO,
571                         P2P_EVENT_GROUP_FORMATION_FAILURE);
572                 wpas_p2p_group_delete(wpa_s,
573                                       P2P_GROUP_REMOVAL_FORMATION_FAILED);
574                 return;
575         }
576
577         wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
578
579         ssid = wpa_s->current_ssid;
580         if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
581                 ssid->mode = WPAS_MODE_P2P_GO;
582                 p2p_group_notif_formation_done(wpa_s->p2p_group);
583                 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
584         }
585
586         persistent = 0;
587         if (ssid) {
588                 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
589                 client = ssid->mode == WPAS_MODE_INFRA;
590                 if (ssid->mode == WPAS_MODE_P2P_GO) {
591                         persistent = ssid->p2p_persistent_group;
592                         os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
593                                   ETH_ALEN);
594                 } else
595                         persistent = wpas_p2p_persistent_group(wpa_s,
596                                                                go_dev_addr,
597                                                                ssid->ssid,
598                                                                ssid->ssid_len);
599         } else {
600                 ssid_txt = "";
601                 client = wpa_s->p2p_group_interface ==
602                         P2P_GROUP_INTERFACE_CLIENT;
603                 os_memset(go_dev_addr, 0, ETH_ALEN);
604         }
605
606         wpa_s->show_group_started = 0;
607         if (client) {
608                 /*
609                  * Indicate event only after successfully completed 4-way
610                  * handshake, i.e., when the interface is ready for data
611                  * packets.
612                  */
613                 wpa_s->show_group_started = 1;
614         } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
615                 char psk[65];
616                 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
617                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
618                         "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
619                         "%s",
620                         wpa_s->ifname, ssid_txt, ssid->frequency, psk,
621                         MAC2STR(go_dev_addr),
622                         persistent ? " [PERSISTENT]" : "");
623                 wpas_p2p_cross_connect_setup(wpa_s);
624                 wpas_p2p_set_group_idle_timeout(wpa_s);
625         } else {
626                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
627                         "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
628                         "go_dev_addr=" MACSTR "%s",
629                         wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
630                         ssid && ssid->passphrase ? ssid->passphrase : "",
631                         MAC2STR(go_dev_addr),
632                         persistent ? " [PERSISTENT]" : "");
633                 wpas_p2p_cross_connect_setup(wpa_s);
634                 wpas_p2p_set_group_idle_timeout(wpa_s);
635         }
636
637         if (persistent)
638                 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
639                                                              ssid, go_dev_addr);
640         if (network_id < 0 && ssid)
641                 network_id = ssid->id;
642         if (!client)
643                 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
644 }
645
646
647 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
648                                            unsigned int freq,
649                                            const u8 *dst, const u8 *src,
650                                            const u8 *bssid,
651                                            const u8 *data, size_t data_len,
652                                            enum offchannel_send_action_result
653                                            result)
654 {
655         enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
656
657         if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
658                 return;
659         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
660                 return;
661
662         switch (result) {
663         case OFFCHANNEL_SEND_ACTION_SUCCESS:
664                 res = P2P_SEND_ACTION_SUCCESS;
665                 break;
666         case OFFCHANNEL_SEND_ACTION_NO_ACK:
667                 res = P2P_SEND_ACTION_NO_ACK;
668                 break;
669         case OFFCHANNEL_SEND_ACTION_FAILED:
670                 res = P2P_SEND_ACTION_FAILED;
671                 break;
672         }
673
674         p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
675
676         if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
677             wpa_s->pending_pd_before_join &&
678             (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
679              os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
680                 wpa_s->pending_pd_before_join = 0;
681                 if (wpa_s->p2p_fallback_to_go_neg) {
682                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
683                                 "during p2p_connect-auto");
684                         wpas_p2p_fallback_to_go_neg(wpa_s, 0);
685                         return;
686                 }
687
688                 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
689                            "join-existing-group operation (no ACK for PD "
690                            "Req)");
691                 wpas_p2p_join_start(wpa_s);
692         }
693 }
694
695
696 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
697                             const u8 *src, const u8 *bssid, const u8 *buf,
698                             size_t len, unsigned int wait_time)
699 {
700         struct wpa_supplicant *wpa_s = ctx;
701         return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
702                                       wait_time,
703                                       wpas_p2p_send_action_tx_status, 1);
704 }
705
706
707 static void wpas_send_action_done(void *ctx)
708 {
709         struct wpa_supplicant *wpa_s = ctx;
710         offchannel_send_action_done(wpa_s);
711 }
712
713
714 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
715                                     struct p2p_go_neg_results *params)
716 {
717         if (wpa_s->go_params == NULL) {
718                 wpa_s->go_params = os_malloc(sizeof(*params));
719                 if (wpa_s->go_params == NULL)
720                         return -1;
721         }
722         os_memcpy(wpa_s->go_params, params, sizeof(*params));
723         return 0;
724 }
725
726
727 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
728                                     struct p2p_go_neg_results *res)
729 {
730         wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
731                    MAC2STR(res->peer_interface_addr));
732         wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
733                           res->ssid, res->ssid_len);
734         wpa_supplicant_ap_deinit(wpa_s);
735         wpas_copy_go_neg_results(wpa_s, res);
736         if (res->wps_method == WPS_PBC)
737                 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
738         else {
739                 u16 dev_pw_id = DEV_PW_DEFAULT;
740                 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
741                         dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
742                 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
743                                    wpa_s->p2p_pin, 1, dev_pw_id);
744         }
745 }
746
747
748 static void p2p_go_configured(void *ctx, void *data)
749 {
750         struct wpa_supplicant *wpa_s = ctx;
751         struct p2p_go_neg_results *params = data;
752         struct wpa_ssid *ssid;
753         int network_id = -1;
754
755         ssid = wpa_s->current_ssid;
756         if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
757                 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
758                 if (wpa_s->global->p2p_group_formation == wpa_s)
759                         wpa_s->global->p2p_group_formation = NULL;
760                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
761                         "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
762                         "go_dev_addr=" MACSTR "%s",
763                         wpa_s->ifname,
764                         wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
765                         ssid->frequency,
766                         params->passphrase ? params->passphrase : "",
767                         MAC2STR(wpa_s->global->p2p_dev_addr),
768                         params->persistent_group ? " [PERSISTENT]" : "");
769
770                 if (params->persistent_group)
771                         network_id = wpas_p2p_store_persistent_group(
772                                 wpa_s->parent, ssid,
773                                 wpa_s->global->p2p_dev_addr);
774                 if (network_id < 0)
775                         network_id = ssid->id;
776                 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
777                 wpas_p2p_cross_connect_setup(wpa_s);
778                 wpas_p2p_set_group_idle_timeout(wpa_s);
779                 return;
780         }
781
782         wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
783         if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
784                                               params->peer_interface_addr)) {
785                 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
786                            "filtering");
787                 return;
788         }
789         if (params->wps_method == WPS_PBC)
790                 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
791                                           params->peer_device_addr);
792         else if (wpa_s->p2p_pin[0])
793                 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
794                                           wpa_s->p2p_pin, NULL, 0);
795         os_free(wpa_s->go_params);
796         wpa_s->go_params = NULL;
797 }
798
799
800 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
801                               struct p2p_go_neg_results *params,
802                               int group_formation)
803 {
804         struct wpa_ssid *ssid;
805
806         if (wpas_copy_go_neg_results(wpa_s, params) < 0)
807                 return;
808
809         ssid = wpa_config_add_network(wpa_s->conf);
810         if (ssid == NULL)
811                 return;
812
813         wpa_s->show_group_started = 0;
814
815         wpa_config_set_network_defaults(ssid);
816         ssid->temporary = 1;
817         ssid->p2p_group = 1;
818         ssid->p2p_persistent_group = params->persistent_group;
819         ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
820                 WPAS_MODE_P2P_GO;
821         ssid->frequency = params->freq;
822         ssid->ht40 = params->ht40;
823         ssid->ssid = os_zalloc(params->ssid_len + 1);
824         if (ssid->ssid) {
825                 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
826                 ssid->ssid_len = params->ssid_len;
827         }
828         ssid->auth_alg = WPA_AUTH_ALG_OPEN;
829         ssid->key_mgmt = WPA_KEY_MGMT_PSK;
830         ssid->proto = WPA_PROTO_RSN;
831         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
832         ssid->passphrase = os_strdup(params->passphrase);
833         ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
834
835         wpa_s->ap_configured_cb = p2p_go_configured;
836         wpa_s->ap_configured_cb_ctx = wpa_s;
837         wpa_s->ap_configured_cb_data = wpa_s->go_params;
838         wpa_s->connect_without_scan = ssid;
839         wpa_s->reassociate = 1;
840         wpa_s->disconnected = 0;
841         wpa_supplicant_req_scan(wpa_s, 0, 0);
842 }
843
844
845 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
846                                   const struct wpa_supplicant *src)
847 {
848         struct wpa_config *d;
849         const struct wpa_config *s;
850
851         d = dst->conf;
852         s = src->conf;
853
854 #define C(n) if (s->n) d->n = os_strdup(s->n)
855         C(device_name);
856         C(manufacturer);
857         C(model_name);
858         C(model_number);
859         C(serial_number);
860         C(config_methods);
861 #undef C
862
863         os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
864         os_memcpy(d->sec_device_type, s->sec_device_type,
865                   sizeof(d->sec_device_type));
866         d->num_sec_device_types = s->num_sec_device_types;
867
868         d->p2p_group_idle = s->p2p_group_idle;
869         d->p2p_intra_bss = s->p2p_intra_bss;
870         d->persistent_reconnect = s->persistent_reconnect;
871         d->max_num_sta = s->max_num_sta;
872         d->pbc_in_m1 = s->pbc_in_m1;
873 }
874
875
876 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
877                                         enum wpa_driver_if_type type)
878 {
879         char ifname[120], force_ifname[120];
880
881         if (wpa_s->pending_interface_name[0]) {
882                 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
883                            "- skip creation of a new one");
884                 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
885                         wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
886                                    "unknown?! ifname='%s'",
887                                    wpa_s->pending_interface_name);
888                         return -1;
889                 }
890                 return 0;
891         }
892
893         os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
894                     wpa_s->p2p_group_idx);
895         if (os_strlen(ifname) >= IFNAMSIZ &&
896             os_strlen(wpa_s->ifname) < IFNAMSIZ) {
897                 /* Try to avoid going over the IFNAMSIZ length limit */
898                 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
899                             wpa_s->p2p_group_idx);
900         }
901         force_ifname[0] = '\0';
902
903         wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
904                    ifname);
905         wpa_s->p2p_group_idx++;
906
907         wpa_s->pending_interface_type = type;
908         if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
909                            wpa_s->pending_interface_addr, NULL) < 0) {
910                 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
911                            "interface");
912                 return -1;
913         }
914
915         if (force_ifname[0]) {
916                 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
917                            force_ifname);
918                 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
919                            sizeof(wpa_s->pending_interface_name));
920         } else
921                 os_strlcpy(wpa_s->pending_interface_name, ifname,
922                            sizeof(wpa_s->pending_interface_name));
923         wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
924                    MACSTR, wpa_s->pending_interface_name,
925                    MAC2STR(wpa_s->pending_interface_addr));
926
927         return 0;
928 }
929
930
931 static void wpas_p2p_remove_pending_group_interface(
932         struct wpa_supplicant *wpa_s)
933 {
934         if (!wpa_s->pending_interface_name[0] ||
935             is_zero_ether_addr(wpa_s->pending_interface_addr))
936                 return; /* No pending virtual interface */
937
938         wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
939                    wpa_s->pending_interface_name);
940         wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
941                           wpa_s->pending_interface_name);
942         os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
943         wpa_s->pending_interface_name[0] = '\0';
944 }
945
946
947 static struct wpa_supplicant *
948 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
949 {
950         struct wpa_interface iface;
951         struct wpa_supplicant *group_wpa_s;
952
953         if (!wpa_s->pending_interface_name[0]) {
954                 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
955                 if (!wpas_p2p_create_iface(wpa_s))
956                         return NULL;
957                 /*
958                  * Something has forced us to remove the pending interface; try
959                  * to create a new one and hope for the best that we will get
960                  * the same local address.
961                  */
962                 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
963                                                  WPA_IF_P2P_CLIENT) < 0)
964                         return NULL;
965         }
966
967         os_memset(&iface, 0, sizeof(iface));
968         iface.ifname = wpa_s->pending_interface_name;
969         iface.driver = wpa_s->driver->name;
970         iface.ctrl_interface = wpa_s->conf->ctrl_interface;
971         iface.driver_param = wpa_s->conf->driver_param;
972         group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
973         if (group_wpa_s == NULL) {
974                 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
975                            "wpa_supplicant interface");
976                 return NULL;
977         }
978         wpa_s->pending_interface_name[0] = '\0';
979         group_wpa_s->parent = wpa_s;
980         group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
981                 P2P_GROUP_INTERFACE_CLIENT;
982         wpa_s->global->p2p_group_formation = group_wpa_s;
983
984         wpas_p2p_clone_config(group_wpa_s, wpa_s);
985
986         return group_wpa_s;
987 }
988
989
990 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
991                                              void *timeout_ctx)
992 {
993         struct wpa_supplicant *wpa_s = eloop_ctx;
994         wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
995         if (wpa_s->global->p2p)
996                 p2p_group_formation_failed(wpa_s->global->p2p);
997         else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
998                 wpa_drv_p2p_group_formation_failed(wpa_s);
999         wpas_group_formation_completed(wpa_s, 0);
1000 }
1001
1002
1003 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1004 {
1005         struct wpa_supplicant *wpa_s = ctx;
1006
1007         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1008                 wpa_drv_cancel_remain_on_channel(wpa_s);
1009                 wpa_s->off_channel_freq = 0;
1010                 wpa_s->roc_waiting_drv_freq = 0;
1011         }
1012
1013         if (res->status) {
1014                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1015                         res->status);
1016                 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1017                 wpas_p2p_remove_pending_group_interface(wpa_s);
1018                 return;
1019         }
1020
1021         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
1022         wpas_notify_p2p_go_neg_completed(wpa_s, res);
1023
1024         if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1025                 struct wpa_ssid *ssid;
1026                 ssid = wpa_config_get_network(wpa_s->conf,
1027                                               wpa_s->p2p_persistent_id);
1028                 if (ssid && ssid->disabled == 2 &&
1029                     ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1030                         size_t len = os_strlen(ssid->passphrase);
1031                         wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1032                                    "on requested persistent group");
1033                         os_memcpy(res->passphrase, ssid->passphrase, len);
1034                         res->passphrase[len] = '\0';
1035                 }
1036         }
1037
1038         if (wpa_s->create_p2p_iface) {
1039                 struct wpa_supplicant *group_wpa_s =
1040                         wpas_p2p_init_group_interface(wpa_s, res->role_go);
1041                 if (group_wpa_s == NULL) {
1042                         wpas_p2p_remove_pending_group_interface(wpa_s);
1043                         return;
1044                 }
1045                 if (group_wpa_s != wpa_s) {
1046                         os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1047                                   sizeof(group_wpa_s->p2p_pin));
1048                         group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1049                 }
1050                 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1051                 wpa_s->pending_interface_name[0] = '\0';
1052                 group_wpa_s->p2p_in_provisioning = 1;
1053
1054                 if (res->role_go)
1055                         wpas_start_wps_go(group_wpa_s, res, 1);
1056                 else
1057                         wpas_start_wps_enrollee(group_wpa_s, res);
1058         } else {
1059                 wpa_s->p2p_in_provisioning = 1;
1060                 wpa_s->global->p2p_group_formation = wpa_s;
1061
1062                 if (res->role_go)
1063                         wpas_start_wps_go(wpa_s, res, 1);
1064                 else
1065                         wpas_start_wps_enrollee(ctx, res);
1066         }
1067
1068         wpa_s->p2p_long_listen = 0;
1069         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1070
1071         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1072         eloop_register_timeout(15 + res->peer_config_timeout / 100,
1073                                (res->peer_config_timeout % 100) * 10000,
1074                                wpas_p2p_group_formation_timeout, wpa_s, NULL);
1075 }
1076
1077
1078 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1079 {
1080         struct wpa_supplicant *wpa_s = ctx;
1081         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1082                 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1083
1084         wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1085 }
1086
1087
1088 void wpas_dev_found(void *ctx, const u8 *addr,
1089                     const struct p2p_peer_info *info,
1090                     int new_device)
1091 {
1092 #ifndef CONFIG_NO_STDOUT_DEBUG
1093         struct wpa_supplicant *wpa_s = ctx;
1094         char devtype[WPS_DEV_TYPE_BUFSIZE];
1095
1096         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1097                 " p2p_dev_addr=" MACSTR
1098                 " pri_dev_type=%s name='%s' config_methods=0x%x "
1099                 "dev_capab=0x%x group_capab=0x%x",
1100                 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1101                 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1102                                      sizeof(devtype)),
1103                 info->device_name, info->config_methods,
1104                 info->dev_capab, info->group_capab);
1105 #endif /* CONFIG_NO_STDOUT_DEBUG */
1106
1107         wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1108 }
1109
1110
1111 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1112 {
1113         struct wpa_supplicant *wpa_s = ctx;
1114
1115         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1116                 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1117
1118         wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1119 }
1120
1121
1122 static int wpas_start_listen(void *ctx, unsigned int freq,
1123                              unsigned int duration,
1124                              const struct wpabuf *probe_resp_ie)
1125 {
1126         struct wpa_supplicant *wpa_s = ctx;
1127
1128         wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1129
1130         if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1131                 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1132                            "report received Probe Request frames");
1133                 return -1;
1134         }
1135
1136         wpa_s->pending_listen_freq = freq;
1137         wpa_s->pending_listen_duration = duration;
1138
1139         if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1140                 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1141                            "to remain on channel (%u MHz) for Listen "
1142                            "state", freq);
1143                 wpa_s->pending_listen_freq = 0;
1144                 return -1;
1145         }
1146         wpa_s->off_channel_freq = 0;
1147         wpa_s->roc_waiting_drv_freq = freq;
1148
1149         return 0;
1150 }
1151
1152
1153 static void wpas_stop_listen(void *ctx)
1154 {
1155         struct wpa_supplicant *wpa_s = ctx;
1156         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1157                 wpa_drv_cancel_remain_on_channel(wpa_s);
1158                 wpa_s->off_channel_freq = 0;
1159                 wpa_s->roc_waiting_drv_freq = 0;
1160         }
1161         wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1162         wpa_drv_probe_req_report(wpa_s, 0);
1163 }
1164
1165
1166 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1167 {
1168         struct wpa_supplicant *wpa_s = ctx;
1169         return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1170 }
1171
1172
1173 static struct p2p_srv_bonjour *
1174 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1175                              const struct wpabuf *query)
1176 {
1177         struct p2p_srv_bonjour *bsrv;
1178         size_t len;
1179
1180         len = wpabuf_len(query);
1181         dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1182                          struct p2p_srv_bonjour, list) {
1183                 if (len == wpabuf_len(bsrv->query) &&
1184                     os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1185                               len) == 0)
1186                         return bsrv;
1187         }
1188         return NULL;
1189 }
1190
1191
1192 static struct p2p_srv_upnp *
1193 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1194                           const char *service)
1195 {
1196         struct p2p_srv_upnp *usrv;
1197
1198         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1199                          struct p2p_srv_upnp, list) {
1200                 if (version == usrv->version &&
1201                     os_strcmp(service, usrv->service) == 0)
1202                         return usrv;
1203         }
1204         return NULL;
1205 }
1206
1207
1208 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1209                                         u8 srv_trans_id)
1210 {
1211         u8 *len_pos;
1212
1213         if (wpabuf_tailroom(resp) < 5)
1214                 return;
1215
1216         /* Length (to be filled) */
1217         len_pos = wpabuf_put(resp, 2);
1218         wpabuf_put_u8(resp, srv_proto);
1219         wpabuf_put_u8(resp, srv_trans_id);
1220         /* Status Code */
1221         wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1222         /* Response Data: empty */
1223         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1224 }
1225
1226
1227 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1228                                 struct wpabuf *resp, u8 srv_trans_id)
1229 {
1230         struct p2p_srv_bonjour *bsrv;
1231         u8 *len_pos;
1232
1233         wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1234
1235         if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1236                 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1237                 return;
1238         }
1239
1240         dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1241                          struct p2p_srv_bonjour, list) {
1242                 if (wpabuf_tailroom(resp) <
1243                     5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1244                         return;
1245                 /* Length (to be filled) */
1246                 len_pos = wpabuf_put(resp, 2);
1247                 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1248                 wpabuf_put_u8(resp, srv_trans_id);
1249                 /* Status Code */
1250                 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1251                 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1252                                   wpabuf_head(bsrv->resp),
1253                                   wpabuf_len(bsrv->resp));
1254                 /* Response Data */
1255                 wpabuf_put_buf(resp, bsrv->query); /* Key */
1256                 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1257                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1258                              2);
1259         }
1260 }
1261
1262
1263 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1264                                 struct wpabuf *resp, u8 srv_trans_id,
1265                                 const u8 *query, size_t query_len)
1266 {
1267         struct p2p_srv_bonjour *bsrv;
1268         struct wpabuf buf;
1269         u8 *len_pos;
1270
1271         wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1272                           query, query_len);
1273         if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1274                 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1275                 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1276                                             srv_trans_id);
1277                 return;
1278         }
1279
1280         if (query_len == 0) {
1281                 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1282                 return;
1283         }
1284
1285         if (wpabuf_tailroom(resp) < 5)
1286                 return;
1287         /* Length (to be filled) */
1288         len_pos = wpabuf_put(resp, 2);
1289         wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1290         wpabuf_put_u8(resp, srv_trans_id);
1291
1292         wpabuf_set(&buf, query, query_len);
1293         bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1294         if (bsrv == NULL) {
1295                 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1296                            "available");
1297
1298                 /* Status Code */
1299                 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1300                 /* Response Data: empty */
1301                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1302                              2);
1303                 return;
1304         }
1305
1306         /* Status Code */
1307         wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1308         wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1309                           wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1310
1311         if (wpabuf_tailroom(resp) >=
1312             wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1313                 /* Response Data */
1314                 wpabuf_put_buf(resp, bsrv->query); /* Key */
1315                 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1316         }
1317         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1318 }
1319
1320
1321 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1322                              struct wpabuf *resp, u8 srv_trans_id)
1323 {
1324         struct p2p_srv_upnp *usrv;
1325         u8 *len_pos;
1326
1327         wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1328
1329         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1330                 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1331                 return;
1332         }
1333
1334         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1335                          struct p2p_srv_upnp, list) {
1336                 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1337                         return;
1338
1339                 /* Length (to be filled) */
1340                 len_pos = wpabuf_put(resp, 2);
1341                 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1342                 wpabuf_put_u8(resp, srv_trans_id);
1343
1344                 /* Status Code */
1345                 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1346                 /* Response Data */
1347                 wpabuf_put_u8(resp, usrv->version);
1348                 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1349                            usrv->service);
1350                 wpabuf_put_str(resp, usrv->service);
1351                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1352                              2);
1353         }
1354 }
1355
1356
1357 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1358                              struct wpabuf *resp, u8 srv_trans_id,
1359                              const u8 *query, size_t query_len)
1360 {
1361         struct p2p_srv_upnp *usrv;
1362         u8 *len_pos;
1363         u8 version;
1364         char *str;
1365         int count = 0;
1366
1367         wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1368                           query, query_len);
1369
1370         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1371                 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1372                 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1373                                             srv_trans_id);
1374                 return;
1375         }
1376
1377         if (query_len == 0) {
1378                 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1379                 return;
1380         }
1381
1382         if (wpabuf_tailroom(resp) < 5)
1383                 return;
1384
1385         /* Length (to be filled) */
1386         len_pos = wpabuf_put(resp, 2);
1387         wpabuf_put_u8(resp, P2P_SERV_UPNP);
1388         wpabuf_put_u8(resp, srv_trans_id);
1389
1390         version = query[0];
1391         str = os_malloc(query_len);
1392         if (str == NULL)
1393                 return;
1394         os_memcpy(str, query + 1, query_len - 1);
1395         str[query_len - 1] = '\0';
1396
1397         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1398                          struct p2p_srv_upnp, list) {
1399                 if (version != usrv->version)
1400                         continue;
1401
1402                 if (os_strcmp(str, "ssdp:all") != 0 &&
1403                     os_strstr(usrv->service, str) == NULL)
1404                         continue;
1405
1406                 if (wpabuf_tailroom(resp) < 2)
1407                         break;
1408                 if (count == 0) {
1409                         /* Status Code */
1410                         wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1411                         /* Response Data */
1412                         wpabuf_put_u8(resp, version);
1413                 } else
1414                         wpabuf_put_u8(resp, ',');
1415
1416                 count++;
1417
1418                 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1419                            usrv->service);
1420                 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1421                         break;
1422                 wpabuf_put_str(resp, usrv->service);
1423         }
1424         os_free(str);
1425
1426         if (count == 0) {
1427                 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1428                            "available");
1429                 /* Status Code */
1430                 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1431                 /* Response Data: empty */
1432         }
1433
1434         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1435 }
1436
1437
1438 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1439                      u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1440 {
1441         struct wpa_supplicant *wpa_s = ctx;
1442         const u8 *pos = tlvs;
1443         const u8 *end = tlvs + tlvs_len;
1444         const u8 *tlv_end;
1445         u16 slen;
1446         struct wpabuf *resp;
1447         u8 srv_proto, srv_trans_id;
1448         size_t buf_len;
1449         char *buf;
1450
1451         wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1452                     tlvs, tlvs_len);
1453         buf_len = 2 * tlvs_len + 1;
1454         buf = os_malloc(buf_len);
1455         if (buf) {
1456                 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1457                 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1458                              MACSTR " %u %u %s",
1459                              freq, MAC2STR(sa), dialog_token, update_indic,
1460                              buf);
1461                 os_free(buf);
1462         }
1463
1464         if (wpa_s->p2p_sd_over_ctrl_iface) {
1465                 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1466                                            update_indic, tlvs, tlvs_len);
1467                 return; /* to be processed by an external program */
1468         }
1469
1470         resp = wpabuf_alloc(10000);
1471         if (resp == NULL)
1472                 return;
1473
1474         while (pos + 1 < end) {
1475                 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1476                 slen = WPA_GET_LE16(pos);
1477                 pos += 2;
1478                 if (pos + slen > end || slen < 2) {
1479                         wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1480                                    "length");
1481                         wpabuf_free(resp);
1482                         return;
1483                 }
1484                 tlv_end = pos + slen;
1485
1486                 srv_proto = *pos++;
1487                 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1488                            srv_proto);
1489                 srv_trans_id = *pos++;
1490                 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1491                            srv_trans_id);
1492
1493                 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1494                             pos, tlv_end - pos);
1495
1496
1497                 if (wpa_s->force_long_sd) {
1498                         wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1499                                    "response");
1500                         wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1501                         wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1502                         goto done;
1503                 }
1504
1505                 switch (srv_proto) {
1506                 case P2P_SERV_ALL_SERVICES:
1507                         wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1508                                    "for all services");
1509                         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1510                             dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1511                                 wpa_printf(MSG_DEBUG, "P2P: No service "
1512                                            "discovery protocols available");
1513                                 wpas_sd_add_proto_not_avail(
1514                                         resp, P2P_SERV_ALL_SERVICES,
1515                                         srv_trans_id);
1516                                 break;
1517                         }
1518                         wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1519                         wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1520                         break;
1521                 case P2P_SERV_BONJOUR:
1522                         wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1523                                             pos, tlv_end - pos);
1524                         break;
1525                 case P2P_SERV_UPNP:
1526                         wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1527                                          pos, tlv_end - pos);
1528                         break;
1529                 default:
1530                         wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1531                                    "protocol %u", srv_proto);
1532                         wpas_sd_add_proto_not_avail(resp, srv_proto,
1533                                                     srv_trans_id);
1534                         break;
1535                 }
1536
1537                 pos = tlv_end;
1538         }
1539
1540 done:
1541         wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1542                                    update_indic, tlvs, tlvs_len);
1543
1544         wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1545
1546         wpabuf_free(resp);
1547 }
1548
1549
1550 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1551                       const u8 *tlvs, size_t tlvs_len)
1552 {
1553         struct wpa_supplicant *wpa_s = ctx;
1554         const u8 *pos = tlvs;
1555         const u8 *end = tlvs + tlvs_len;
1556         const u8 *tlv_end;
1557         u16 slen;
1558         size_t buf_len;
1559         char *buf;
1560
1561         wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1562                     tlvs, tlvs_len);
1563         if (tlvs_len > 1500) {
1564                 /* TODO: better way for handling this */
1565                 wpa_msg_ctrl(wpa_s, MSG_INFO,
1566                              P2P_EVENT_SERV_DISC_RESP MACSTR
1567                              " %u <long response: %u bytes>",
1568                              MAC2STR(sa), update_indic,
1569                              (unsigned int) tlvs_len);
1570         } else {
1571                 buf_len = 2 * tlvs_len + 1;
1572                 buf = os_malloc(buf_len);
1573                 if (buf) {
1574                         wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1575                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1576                                      P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1577                                      MAC2STR(sa), update_indic, buf);
1578                         os_free(buf);
1579                 }
1580         }
1581
1582         while (pos < end) {
1583                 u8 srv_proto, srv_trans_id, status;
1584
1585                 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1586                 slen = WPA_GET_LE16(pos);
1587                 pos += 2;
1588                 if (pos + slen > end || slen < 3) {
1589                         wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1590                                    "length");
1591                         return;
1592                 }
1593                 tlv_end = pos + slen;
1594
1595                 srv_proto = *pos++;
1596                 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1597                            srv_proto);
1598                 srv_trans_id = *pos++;
1599                 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1600                            srv_trans_id);
1601                 status = *pos++;
1602                 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1603                            status);
1604
1605                 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1606                             pos, tlv_end - pos);
1607
1608                 pos = tlv_end;
1609         }
1610
1611         wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1612 }
1613
1614
1615 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1616                         const struct wpabuf *tlvs)
1617 {
1618         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1619                 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
1620         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1621                 return 0;
1622         return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1623 }
1624
1625
1626 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1627                              u8 version, const char *query)
1628 {
1629         struct wpabuf *tlvs;
1630         u64 ret;
1631
1632         tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1633         if (tlvs == NULL)
1634                 return 0;
1635         wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1636         wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1637         wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1638         wpabuf_put_u8(tlvs, version);
1639         wpabuf_put_str(tlvs, query);
1640         ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1641         wpabuf_free(tlvs);
1642         return ret;
1643 }
1644
1645
1646 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
1647 {
1648         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1649                 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
1650         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1651                 return -1;
1652         return p2p_sd_cancel_request(wpa_s->global->p2p,
1653                                      (void *) (uintptr_t) req);
1654 }
1655
1656
1657 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1658                           const u8 *dst, u8 dialog_token,
1659                           const struct wpabuf *resp_tlvs)
1660 {
1661         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1662                 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1663                                         resp_tlvs);
1664                 return;
1665         }
1666         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1667                 return;
1668         p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1669                         resp_tlvs);
1670 }
1671
1672
1673 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1674 {
1675         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1676                 wpa_drv_p2p_service_update(wpa_s);
1677                 return;
1678         }
1679         if (wpa_s->global->p2p)
1680                 p2p_sd_service_update(wpa_s->global->p2p);
1681 }
1682
1683
1684 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1685 {
1686         dl_list_del(&bsrv->list);
1687         wpabuf_free(bsrv->query);
1688         wpabuf_free(bsrv->resp);
1689         os_free(bsrv);
1690 }
1691
1692
1693 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1694 {
1695         dl_list_del(&usrv->list);
1696         os_free(usrv->service);
1697         os_free(usrv);
1698 }
1699
1700
1701 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1702 {
1703         struct p2p_srv_bonjour *bsrv, *bn;
1704         struct p2p_srv_upnp *usrv, *un;
1705
1706         dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1707                               struct p2p_srv_bonjour, list)
1708                 wpas_p2p_srv_bonjour_free(bsrv);
1709
1710         dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1711                               struct p2p_srv_upnp, list)
1712                 wpas_p2p_srv_upnp_free(usrv);
1713
1714         wpas_p2p_sd_service_update(wpa_s);
1715 }
1716
1717
1718 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1719                                  struct wpabuf *query, struct wpabuf *resp)
1720 {
1721         struct p2p_srv_bonjour *bsrv;
1722
1723         bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1724         if (bsrv) {
1725                 wpabuf_free(query);
1726                 wpabuf_free(bsrv->resp);
1727                 bsrv->resp = resp;
1728                 return 0;
1729         }
1730
1731         bsrv = os_zalloc(sizeof(*bsrv));
1732         if (bsrv == NULL)
1733                 return -1;
1734         bsrv->query = query;
1735         bsrv->resp = resp;
1736         dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1737
1738         wpas_p2p_sd_service_update(wpa_s);
1739         return 0;
1740 }
1741
1742
1743 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1744                                  const struct wpabuf *query)
1745 {
1746         struct p2p_srv_bonjour *bsrv;
1747
1748         bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1749         if (bsrv == NULL)
1750                 return -1;
1751         wpas_p2p_srv_bonjour_free(bsrv);
1752         wpas_p2p_sd_service_update(wpa_s);
1753         return 0;
1754 }
1755
1756
1757 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1758                               const char *service)
1759 {
1760         struct p2p_srv_upnp *usrv;
1761
1762         if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1763                 return 0; /* Already listed */
1764         usrv = os_zalloc(sizeof(*usrv));
1765         if (usrv == NULL)
1766                 return -1;
1767         usrv->version = version;
1768         usrv->service = os_strdup(service);
1769         if (usrv->service == NULL) {
1770                 os_free(usrv);
1771                 return -1;
1772         }
1773         dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1774
1775         wpas_p2p_sd_service_update(wpa_s);
1776         return 0;
1777 }
1778
1779
1780 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1781                               const char *service)
1782 {
1783         struct p2p_srv_upnp *usrv;
1784
1785         usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1786         if (usrv == NULL)
1787                 return -1;
1788         wpas_p2p_srv_upnp_free(usrv);
1789         wpas_p2p_sd_service_update(wpa_s);
1790         return 0;
1791 }
1792
1793
1794 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
1795                                          const u8 *peer, const char *params,
1796                                          unsigned int generated_pin)
1797 {
1798         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
1799                 MAC2STR(peer), generated_pin, params);
1800 }
1801
1802
1803 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1804                                         const u8 *peer, const char *params)
1805 {
1806         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1807                 MAC2STR(peer), params);
1808 }
1809
1810
1811 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1812                         const u8 *dev_addr, const u8 *pri_dev_type,
1813                         const char *dev_name, u16 supp_config_methods,
1814                         u8 dev_capab, u8 group_capab, const u8 *group_id,
1815                         size_t group_id_len)
1816 {
1817         struct wpa_supplicant *wpa_s = ctx;
1818         char devtype[WPS_DEV_TYPE_BUFSIZE];
1819         char params[300];
1820         u8 empty_dev_type[8];
1821         unsigned int generated_pin = 0;
1822         struct wpa_supplicant *group = NULL;
1823
1824         if (group_id) {
1825                 for (group = wpa_s->global->ifaces; group; group = group->next)
1826                 {
1827                         struct wpa_ssid *s = group->current_ssid;
1828                         if (s != NULL &&
1829                             s->mode == WPAS_MODE_P2P_GO &&
1830                             group_id_len - ETH_ALEN == s->ssid_len &&
1831                             os_memcmp(group_id + ETH_ALEN, s->ssid,
1832                                       s->ssid_len) == 0)
1833                                 break;
1834                 }
1835         }
1836
1837         if (pri_dev_type == NULL) {
1838                 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1839                 pri_dev_type = empty_dev_type;
1840         }
1841         os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1842                     " pri_dev_type=%s name='%s' config_methods=0x%x "
1843                     "dev_capab=0x%x group_capab=0x%x%s%s",
1844                     MAC2STR(dev_addr),
1845                     wps_dev_type_bin2str(pri_dev_type, devtype,
1846                                          sizeof(devtype)),
1847                     dev_name, supp_config_methods, dev_capab, group_capab,
1848                     group ? " group=" : "",
1849                     group ? group->ifname : "");
1850         params[sizeof(params) - 1] = '\0';
1851
1852         if (config_methods & WPS_CONFIG_DISPLAY) {
1853                 generated_pin = wps_generate_pin();
1854                 wpas_prov_disc_local_display(wpa_s, peer, params,
1855                                              generated_pin);
1856         } else if (config_methods & WPS_CONFIG_KEYPAD)
1857                 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1858         else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1859                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1860                         "%s", MAC2STR(peer), params);
1861
1862         wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
1863                                             P2P_PROV_DISC_SUCCESS,
1864                                             config_methods, generated_pin);
1865 }
1866
1867
1868 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1869 {
1870         struct wpa_supplicant *wpa_s = ctx;
1871         unsigned int generated_pin = 0;
1872         char params[20];
1873
1874         if (wpa_s->pending_pd_before_join &&
1875             (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1876              os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
1877                 wpa_s->pending_pd_before_join = 0;
1878                 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
1879                            "join-existing-group operation");
1880                 wpas_p2p_join_start(wpa_s);
1881                 return;
1882         }
1883
1884         if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
1885             wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
1886                 os_snprintf(params, sizeof(params), " peer_go=%d",
1887                             wpa_s->pending_pd_use == AUTO_PD_JOIN);
1888         else
1889                 params[0] = '\0';
1890
1891         if (config_methods & WPS_CONFIG_DISPLAY)
1892                 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1893         else if (config_methods & WPS_CONFIG_KEYPAD) {
1894                 generated_pin = wps_generate_pin();
1895                 wpas_prov_disc_local_display(wpa_s, peer, params,
1896                                              generated_pin);
1897         } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1898                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
1899                         "%s", MAC2STR(peer), params);
1900
1901         wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1902                                             P2P_PROV_DISC_SUCCESS,
1903                                             config_methods, generated_pin);
1904 }
1905
1906
1907 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
1908                                 enum p2p_prov_disc_status status)
1909 {
1910         struct wpa_supplicant *wpa_s = ctx;
1911
1912         if (wpa_s->p2p_fallback_to_go_neg) {
1913                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
1914                         "failed - fall back to GO Negotiation");
1915                 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1916                 return;
1917         }
1918
1919         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
1920                 " p2p_dev_addr=" MACSTR " status=%d",
1921                 MAC2STR(peer), status);
1922
1923         wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1924                                             status, 0, 0);
1925 }
1926
1927
1928 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
1929                                   const u8 *go_dev_addr, const u8 *ssid,
1930                                   size_t ssid_len, int *go, u8 *group_bssid,
1931                                   int *force_freq, int persistent_group)
1932 {
1933         struct wpa_supplicant *wpa_s = ctx;
1934         struct wpa_ssid *s;
1935         u8 cur_bssid[ETH_ALEN];
1936         int res;
1937         struct wpa_supplicant *grp;
1938
1939         if (!persistent_group) {
1940                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1941                            " to join an active group", MAC2STR(sa));
1942                 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
1943                     (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
1944                      == 0 ||
1945                      os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
1946                         wpa_printf(MSG_DEBUG, "P2P: Accept previously "
1947                                    "authorized invitation");
1948                         goto accept_inv;
1949                 }
1950                 /*
1951                  * Do not accept the invitation automatically; notify user and
1952                  * request approval.
1953                  */
1954                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1955         }
1956
1957         grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
1958         if (grp) {
1959                 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
1960                            "running persistent group");
1961                 if (*go)
1962                         os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
1963                 goto accept_inv;
1964         }
1965
1966         if (!wpa_s->conf->persistent_reconnect)
1967                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1968
1969         for (s = wpa_s->conf->ssid; s; s = s->next) {
1970                 if (s->disabled == 2 &&
1971                     os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
1972                     s->ssid_len == ssid_len &&
1973                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
1974                         break;
1975         }
1976
1977         if (!s) {
1978                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1979                            " requested reinvocation of an unknown group",
1980                            MAC2STR(sa));
1981                 return P2P_SC_FAIL_UNKNOWN_GROUP;
1982         }
1983
1984         if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
1985                 *go = 1;
1986                 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1987                         wpa_printf(MSG_DEBUG, "P2P: The only available "
1988                                    "interface is already in use - reject "
1989                                    "invitation");
1990                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1991                 }
1992                 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
1993         } else if (s->mode == WPAS_MODE_P2P_GO) {
1994                 *go = 1;
1995                 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
1996                 {
1997                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
1998                                    "interface address for the group");
1999                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2000                 }
2001                 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2002                           ETH_ALEN);
2003         }
2004
2005 accept_inv:
2006         if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2007             wpa_s->assoc_freq) {
2008                 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2009                            "the channel we are already using");
2010                 *force_freq = wpa_s->assoc_freq;
2011         }
2012
2013         res = wpa_drv_shared_freq(wpa_s);
2014         if (res > 0) {
2015                 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2016                            "with the channel we are already using on a "
2017                            "shared interface");
2018                 *force_freq = res;
2019         }
2020
2021         return P2P_SC_SUCCESS;
2022 }
2023
2024
2025 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2026                                      const u8 *ssid, size_t ssid_len,
2027                                      const u8 *go_dev_addr, u8 status,
2028                                      int op_freq)
2029 {
2030         struct wpa_supplicant *wpa_s = ctx;
2031         struct wpa_ssid *s;
2032
2033         for (s = wpa_s->conf->ssid; s; s = s->next) {
2034                 if (s->disabled == 2 &&
2035                     s->ssid_len == ssid_len &&
2036                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
2037                         break;
2038         }
2039
2040         if (status == P2P_SC_SUCCESS) {
2041                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2042                            " was accepted; op_freq=%d MHz",
2043                            MAC2STR(sa), op_freq);
2044                 if (s) {
2045                         wpas_p2p_group_add_persistent(
2046                                 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0, 0);
2047                 } else if (bssid) {
2048                         wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2049                                       wpa_s->p2p_wps_method, 0);
2050                 }
2051                 return;
2052         }
2053
2054         if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2055                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2056                            " was rejected (status %u)", MAC2STR(sa), status);
2057                 return;
2058         }
2059
2060         if (!s) {
2061                 if (bssid) {
2062                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2063                                 "sa=" MACSTR " go_dev_addr=" MACSTR
2064                                 " bssid=" MACSTR " unknown-network",
2065                                 MAC2STR(sa), MAC2STR(go_dev_addr),
2066                                 MAC2STR(bssid));
2067                 } else {
2068                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2069                                 "sa=" MACSTR " go_dev_addr=" MACSTR
2070                                 " unknown-network",
2071                                 MAC2STR(sa), MAC2STR(go_dev_addr));
2072                 }
2073                 return;
2074         }
2075
2076         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2077                 " persistent=%d", MAC2STR(sa), s->id);
2078 }
2079
2080
2081 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2082 {
2083         struct wpa_supplicant *wpa_s = ctx;
2084         struct wpa_ssid *ssid;
2085
2086         if (bssid) {
2087                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2088                         "status=%d " MACSTR,
2089                         status, MAC2STR(bssid));
2090         } else {
2091                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2092                         "status=%d ", status);
2093         }
2094         wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2095
2096         if (wpa_s->pending_invite_ssid_id == -1)
2097                 return; /* Invitation to active group */
2098
2099         if (status != P2P_SC_SUCCESS) {
2100                 wpas_p2p_remove_pending_group_interface(wpa_s);
2101                 return;
2102         }
2103
2104         ssid = wpa_config_get_network(wpa_s->conf,
2105                                       wpa_s->pending_invite_ssid_id);
2106         if (ssid == NULL) {
2107                 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2108                            "data matching with invitation");
2109                 return;
2110         }
2111
2112         wpas_p2p_group_add_persistent(wpa_s, ssid,
2113                                       ssid->mode == WPAS_MODE_P2P_GO, 0, 0);
2114 }
2115
2116
2117 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2118                                     unsigned int freq)
2119 {
2120         unsigned int i;
2121
2122         if (global->p2p_disallow_freq == NULL)
2123                 return 0;
2124
2125         for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2126                 if (freq >= global->p2p_disallow_freq[i].min &&
2127                     freq <= global->p2p_disallow_freq[i].max)
2128                         return 1;
2129         }
2130
2131         return 0;
2132 }
2133
2134
2135 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2136 {
2137         reg->channel[reg->channels] = chan;
2138         reg->channels++;
2139 }
2140
2141
2142 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2143                                      struct p2p_channels *chan)
2144 {
2145         int i, cla = 0;
2146
2147         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2148                    "band");
2149
2150         /* Operating class 81 - 2.4 GHz band channels 1..13 */
2151         chan->reg_class[cla].reg_class = 81;
2152         chan->reg_class[cla].channels = 0;
2153         for (i = 0; i < 11; i++) {
2154                 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2155                         wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2156         }
2157         if (chan->reg_class[cla].channels)
2158                 cla++;
2159
2160         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2161                    "band");
2162
2163         /* Operating class 115 - 5 GHz, channels 36-48 */
2164         chan->reg_class[cla].reg_class = 115;
2165         chan->reg_class[cla].channels = 0;
2166         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2167                 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2168         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2169                 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2170         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2171                 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2172         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2173                 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2174         if (chan->reg_class[cla].channels)
2175                 cla++;
2176
2177         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2178                    "band");
2179
2180         /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2181         chan->reg_class[cla].reg_class = 124;
2182         chan->reg_class[cla].channels = 0;
2183         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2184                 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2185         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2186                 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2187         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2188                 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2189         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2190                 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2191         if (chan->reg_class[cla].channels)
2192                 cla++;
2193
2194         chan->reg_classes = cla;
2195         return 0;
2196 }
2197
2198
2199 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2200                                           u16 num_modes,
2201                                           enum hostapd_hw_mode mode)
2202 {
2203         u16 i;
2204
2205         for (i = 0; i < num_modes; i++) {
2206                 if (modes[i].mode == mode)
2207                         return &modes[i];
2208         }
2209
2210         return NULL;
2211 }
2212
2213
2214 static int has_channel(struct wpa_global *global,
2215                        struct hostapd_hw_modes *mode, u8 chan, int *flags)
2216 {
2217         int i;
2218         unsigned int freq;
2219
2220         freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2221                 chan * 5;
2222         if (wpas_p2p_disallowed_freq(global, freq))
2223                 return 0;
2224
2225         for (i = 0; i < mode->num_channels; i++) {
2226                 if (mode->channels[i].chan == chan) {
2227                         if (flags)
2228                                 *flags = mode->channels[i].flag;
2229                         return !(mode->channels[i].flag &
2230                                  (HOSTAPD_CHAN_DISABLED |
2231                                   HOSTAPD_CHAN_PASSIVE_SCAN |
2232                                   HOSTAPD_CHAN_NO_IBSS |
2233                                   HOSTAPD_CHAN_RADAR));
2234                 }
2235         }
2236
2237         return 0;
2238 }
2239
2240
2241 struct p2p_oper_class_map {
2242         enum hostapd_hw_mode mode;
2243         u8 op_class;
2244         u8 min_chan;
2245         u8 max_chan;
2246         u8 inc;
2247         enum { BW20, BW40PLUS, BW40MINUS } bw;
2248 };
2249
2250 static struct p2p_oper_class_map op_class[] = {
2251         { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2252         { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2253 #if 0 /* Do not enable HT40 on 2 GHz for now */
2254         { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2255         { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2256 #endif
2257         { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2258         { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2259         { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2260         { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2261         { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2262         { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2263         { -1, 0, 0, 0, 0, BW20 }
2264 };
2265
2266
2267 static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2268                                    struct hostapd_hw_modes *mode,
2269                                    u8 channel, u8 bw)
2270 {
2271         int flag;
2272
2273         if (!has_channel(wpa_s->global, mode, channel, &flag))
2274                 return -1;
2275         if (bw == BW40MINUS &&
2276             (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2277              !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2278                 return 0;
2279         if (bw == BW40PLUS &&
2280             (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2281              !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2282                 return 0;
2283         return 1;
2284 }
2285
2286
2287 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2288                                    struct p2p_channels *chan)
2289 {
2290         struct hostapd_hw_modes *mode;
2291         int cla, op;
2292
2293         if (wpa_s->hw.modes == NULL) {
2294                 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2295                            "of all supported channels; assume dualband "
2296                            "support");
2297                 return wpas_p2p_default_channels(wpa_s, chan);
2298         }
2299
2300         cla = 0;
2301
2302         for (op = 0; op_class[op].op_class; op++) {
2303                 struct p2p_oper_class_map *o = &op_class[op];
2304                 u8 ch;
2305                 struct p2p_reg_class *reg = NULL;
2306
2307                 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
2308                 if (mode == NULL)
2309                         continue;
2310                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2311                         if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
2312                                 continue;
2313                         if (reg == NULL) {
2314                                 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2315                                            "class %u", o->op_class);
2316                                 reg = &chan->reg_class[cla];
2317                                 cla++;
2318                                 reg->reg_class = o->op_class;
2319                         }
2320                         reg->channel[reg->channels] = ch;
2321                         reg->channels++;
2322                 }
2323                 if (reg) {
2324                         wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2325                                     reg->channel, reg->channels);
2326                 }
2327         }
2328
2329         chan->reg_classes = cla;
2330
2331         return 0;
2332 }
2333
2334
2335 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2336                            struct hostapd_hw_modes *mode, u8 channel)
2337 {
2338         int op, ret;
2339
2340         for (op = 0; op_class[op].op_class; op++) {
2341                 struct p2p_oper_class_map *o = &op_class[op];
2342                 u8 ch;
2343
2344                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2345                         if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2346                             o->bw == BW20 || ch != channel)
2347                                 continue;
2348                         ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2349                         if (ret < 0)
2350                                 continue;
2351                         else if (ret > 0)
2352                                 return (o->bw == BW40MINUS) ? -1 : 1;
2353                         else
2354                                 return 0;
2355                 }
2356         }
2357         return 0;
2358 }
2359
2360
2361 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2362                         size_t buf_len)
2363 {
2364         struct wpa_supplicant *wpa_s = ctx;
2365
2366         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2367                 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2368                         break;
2369         }
2370         if (wpa_s == NULL)
2371                 return -1;
2372
2373         return wpa_drv_get_noa(wpa_s, buf, buf_len);
2374 }
2375
2376
2377 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2378 {
2379         struct wpa_supplicant *wpa_s = ctx;
2380
2381         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2382                 struct wpa_ssid *ssid = wpa_s->current_ssid;
2383                 if (ssid == NULL)
2384                         continue;
2385                 if (ssid->mode != WPAS_MODE_INFRA)
2386                         continue;
2387                 if (wpa_s->wpa_state != WPA_COMPLETED &&
2388                     wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2389                         continue;
2390                 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2391                         return 1;
2392         }
2393
2394         return 0;
2395 }
2396
2397
2398 /**
2399  * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2400  * @global: Pointer to global data from wpa_supplicant_init()
2401  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2402  * Returns: 0 on success, -1 on failure
2403  */
2404 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2405 {
2406         struct p2p_config p2p;
2407         unsigned int r;
2408         int i;
2409
2410         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2411                 return 0;
2412
2413         if (global->p2p)
2414                 return 0;
2415
2416         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2417                 struct p2p_params params;
2418
2419                 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2420                 os_memset(&params, 0, sizeof(params));
2421                 params.dev_name = wpa_s->conf->device_name;
2422                 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2423                           WPS_DEV_TYPE_LEN);
2424                 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2425                 os_memcpy(params.sec_dev_type,
2426                           wpa_s->conf->sec_device_type,
2427                           params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2428
2429                 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2430                         return -1;
2431
2432                 return 0;
2433         }
2434
2435         os_memset(&p2p, 0, sizeof(p2p));
2436         p2p.msg_ctx = wpa_s;
2437         p2p.cb_ctx = wpa_s;
2438         p2p.p2p_scan = wpas_p2p_scan;
2439         p2p.send_action = wpas_send_action;
2440         p2p.send_action_done = wpas_send_action_done;
2441         p2p.go_neg_completed = wpas_go_neg_completed;
2442         p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2443         p2p.dev_found = wpas_dev_found;
2444         p2p.dev_lost = wpas_dev_lost;
2445         p2p.start_listen = wpas_start_listen;
2446         p2p.stop_listen = wpas_stop_listen;
2447         p2p.send_probe_resp = wpas_send_probe_resp;
2448         p2p.sd_request = wpas_sd_request;
2449         p2p.sd_response = wpas_sd_response;
2450         p2p.prov_disc_req = wpas_prov_disc_req;
2451         p2p.prov_disc_resp = wpas_prov_disc_resp;
2452         p2p.prov_disc_fail = wpas_prov_disc_fail;
2453         p2p.invitation_process = wpas_invitation_process;
2454         p2p.invitation_received = wpas_invitation_received;
2455         p2p.invitation_result = wpas_invitation_result;
2456         p2p.get_noa = wpas_get_noa;
2457         p2p.go_connected = wpas_go_connected;
2458
2459         os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2460         os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
2461         p2p.dev_name = wpa_s->conf->device_name;
2462         p2p.manufacturer = wpa_s->conf->manufacturer;
2463         p2p.model_name = wpa_s->conf->model_name;
2464         p2p.model_number = wpa_s->conf->model_number;
2465         p2p.serial_number = wpa_s->conf->serial_number;
2466         if (wpa_s->wps) {
2467                 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2468                 p2p.config_methods = wpa_s->wps->config_methods;
2469         }
2470
2471         if (wpa_s->conf->p2p_listen_reg_class &&
2472             wpa_s->conf->p2p_listen_channel) {
2473                 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2474                 p2p.channel = wpa_s->conf->p2p_listen_channel;
2475         } else {
2476                 p2p.reg_class = 81;
2477                 /*
2478                  * Pick one of the social channels randomly as the listen
2479                  * channel.
2480                  */
2481                 os_get_random((u8 *) &r, sizeof(r));
2482                 p2p.channel = 1 + (r % 3) * 5;
2483         }
2484         wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2485
2486         if (wpa_s->conf->p2p_oper_reg_class &&
2487             wpa_s->conf->p2p_oper_channel) {
2488                 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2489                 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2490                 p2p.cfg_op_channel = 1;
2491                 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2492                            "%d:%d", p2p.op_reg_class, p2p.op_channel);
2493
2494         } else {
2495                 p2p.op_reg_class = 81;
2496                 /*
2497                  * Use random operation channel from (1, 6, 11) if no other
2498                  * preference is indicated.
2499                  */
2500                 os_get_random((u8 *) &r, sizeof(r));
2501                 p2p.op_channel = 1 + (r % 3) * 5;
2502                 p2p.cfg_op_channel = 0;
2503                 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2504                            "%d:%d", p2p.op_reg_class, p2p.op_channel);
2505         }
2506         if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2507                 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2508                 p2p.country[2] = 0x04;
2509         } else
2510                 os_memcpy(p2p.country, "XX\x04", 3);
2511
2512         if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2513                 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2514                            "channel list");
2515                 return -1;
2516         }
2517
2518         os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2519                   WPS_DEV_TYPE_LEN);
2520
2521         p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2522         os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2523                   p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2524
2525         p2p.concurrent_operations = !!(wpa_s->drv_flags &
2526                                        WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2527
2528         p2p.max_peers = 100;
2529
2530         if (wpa_s->conf->p2p_ssid_postfix) {
2531                 p2p.ssid_postfix_len =
2532                         os_strlen(wpa_s->conf->p2p_ssid_postfix);
2533                 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2534                         p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2535                 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2536                           p2p.ssid_postfix_len);
2537         }
2538
2539         p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2540
2541         global->p2p = p2p_init(&p2p);
2542         if (global->p2p == NULL)
2543                 return -1;
2544         global->p2p_init_wpa_s = wpa_s;
2545
2546         for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2547                 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2548                         continue;
2549                 p2p_add_wps_vendor_extension(
2550                         global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2551         }
2552
2553         return 0;
2554 }
2555
2556
2557 /**
2558  * wpas_p2p_deinit - Deinitialize per-interface P2P data
2559  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2560  *
2561  * This function deinitialize per-interface P2P data.
2562  */
2563 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2564 {
2565         if (wpa_s->driver && wpa_s->drv_priv)
2566                 wpa_drv_probe_req_report(wpa_s, 0);
2567
2568         if (wpa_s->go_params) {
2569                 /* Clear any stored provisioning info */
2570                 p2p_clear_provisioning_info(
2571                         wpa_s->global->p2p,
2572                         wpa_s->go_params->peer_device_addr);
2573         }
2574
2575         os_free(wpa_s->go_params);
2576         wpa_s->go_params = NULL;
2577         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2578         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2579         eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
2580         wpa_s->p2p_long_listen = 0;
2581         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2582         eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2583         wpas_p2p_remove_pending_group_interface(wpa_s);
2584
2585         /* TODO: remove group interface from the driver if this wpa_s instance
2586          * is on top of a P2P group interface */
2587 }
2588
2589
2590 /**
2591  * wpas_p2p_deinit_global - Deinitialize global P2P module
2592  * @global: Pointer to global data from wpa_supplicant_init()
2593  *
2594  * This function deinitializes the global (per device) P2P module.
2595  */
2596 void wpas_p2p_deinit_global(struct wpa_global *global)
2597 {
2598         struct wpa_supplicant *wpa_s, *tmp;
2599
2600         if (global->p2p == NULL)
2601                 return;
2602
2603         /* Remove remaining P2P group interfaces */
2604         wpa_s = global->ifaces;
2605         if (wpa_s)
2606                 wpas_p2p_service_flush(wpa_s);
2607         while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2608                 wpa_s = wpa_s->next;
2609         while (wpa_s) {
2610                 tmp = global->ifaces;
2611                 while (tmp &&
2612                        (tmp == wpa_s ||
2613                         tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2614                         tmp = tmp->next;
2615                 }
2616                 if (tmp == NULL)
2617                         break;
2618                 /* Disconnect from the P2P group and deinit the interface */
2619                 wpas_p2p_disconnect(tmp);
2620         }
2621
2622         /*
2623          * Deinit GO data on any possibly remaining interface (if main
2624          * interface is used as GO).
2625          */
2626         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2627                 if (wpa_s->ap_iface)
2628                         wpas_p2p_group_deinit(wpa_s);
2629         }
2630
2631         p2p_deinit(global->p2p);
2632         global->p2p = NULL;
2633         global->p2p_init_wpa_s = NULL;
2634 }
2635
2636
2637 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2638 {
2639         if (wpa_s->drv_flags &
2640             (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2641              WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2642                 return 1; /* P2P group requires a new interface in every case
2643                            */
2644         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2645                 return 0; /* driver does not support concurrent operations */
2646         if (wpa_s->global->ifaces->next)
2647                 return 1; /* more that one interface already in use */
2648         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2649                 return 1; /* this interface is already in use */
2650         return 0;
2651 }
2652
2653
2654 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2655                                  const u8 *peer_addr,
2656                                  enum p2p_wps_method wps_method,
2657                                  int go_intent, const u8 *own_interface_addr,
2658                                  unsigned int force_freq, int persistent_group,
2659                                  struct wpa_ssid *ssid)
2660 {
2661         if (persistent_group && wpa_s->conf->persistent_reconnect)
2662                 persistent_group = 2;
2663
2664         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2665                 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2666                                            go_intent, own_interface_addr,
2667                                            force_freq, persistent_group);
2668         }
2669
2670         return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2671                            go_intent, own_interface_addr, force_freq,
2672                            persistent_group, ssid ? ssid->ssid : NULL,
2673                            ssid ? ssid->ssid_len : 0,
2674                            wpa_s->p2p_pd_before_go_neg);
2675 }
2676
2677
2678 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2679                                 const u8 *peer_addr,
2680                                 enum p2p_wps_method wps_method,
2681                                 int go_intent, const u8 *own_interface_addr,
2682                                 unsigned int force_freq, int persistent_group,
2683                                 struct wpa_ssid *ssid)
2684 {
2685         if (persistent_group && wpa_s->conf->persistent_reconnect)
2686                 persistent_group = 2;
2687
2688         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2689                 return -1;
2690
2691         return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2692                              go_intent, own_interface_addr, force_freq,
2693                              persistent_group, ssid ? ssid->ssid : NULL,
2694                              ssid ? ssid->ssid_len : 0);
2695 }
2696
2697
2698 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2699 {
2700         wpa_s->p2p_join_scan_count++;
2701         wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2702                    wpa_s->p2p_join_scan_count);
2703         if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2704                 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2705                            " for join operationg - stop join attempt",
2706                            MAC2STR(wpa_s->pending_join_iface_addr));
2707                 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2708                 if (wpa_s->p2p_auto_pd) {
2709                         wpa_s->p2p_auto_pd = 0;
2710                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2711                                 " p2p_dev_addr=" MACSTR " status=N/A",
2712                                 MAC2STR(wpa_s->pending_join_dev_addr));
2713                         return;
2714                 }
2715                 wpa_msg(wpa_s->parent, MSG_INFO,
2716                         P2P_EVENT_GROUP_FORMATION_FAILURE);
2717         }
2718 }
2719
2720
2721 static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
2722 {
2723         struct wpa_supplicant *wpa_s = eloop_ctx;
2724         if (!wpa_s->pending_pd_before_join)
2725                 return;
2726         /*
2727          * Provision Discovery Response may have been lost - try to connect
2728          * anyway since we do not need any information from this PD.
2729          */
2730         wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
2731                    "try to connect anyway");
2732         wpas_p2p_join_start(wpa_s);
2733 }
2734
2735
2736 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
2737 {
2738         struct wpa_supplicant *iface;
2739         int shared_freq;
2740         u8 bssid[ETH_ALEN];
2741
2742         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2743                 return 0;
2744
2745         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
2746                 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
2747                         continue;
2748                 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
2749                         continue;
2750                 if (iface->current_ssid->mode == WPAS_MODE_AP ||
2751                     iface->current_ssid->mode == WPAS_MODE_P2P_GO)
2752                         shared_freq = iface->current_ssid->frequency;
2753                 else if (wpa_drv_get_bssid(iface, bssid) == 0)
2754                         shared_freq = iface->assoc_freq;
2755                 else
2756                         shared_freq = 0;
2757
2758                 if (shared_freq && freq != shared_freq) {
2759                         wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
2760                                    "connected on %d MHz - new connection on "
2761                                    "%d MHz", iface->ifname, shared_freq, freq);
2762                         return 1;
2763                 }
2764         }
2765
2766         shared_freq = wpa_drv_shared_freq(wpa_s);
2767         if (shared_freq > 0 && shared_freq != freq) {
2768                 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
2769                            "virtual interface connected on %d MHz - new "
2770                            "connection on %d MHz", shared_freq, freq);
2771                 return 1;
2772         }
2773
2774         return 0;
2775 }
2776
2777
2778 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
2779                             const u8 *peer_dev_addr)
2780 {
2781         struct wpa_bss *bss;
2782         int updated;
2783
2784         bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
2785         if (bss == NULL)
2786                 return -1;
2787         if (bss->last_update_idx < wpa_s->bss_update_idx) {
2788                 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
2789                            "last scan");
2790                 return 0;
2791         }
2792
2793         updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
2794         wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
2795                    "%ld.%06ld (%supdated in last scan)",
2796                    bss->last_update.sec, bss->last_update.usec,
2797                    updated ? "": "not ");
2798
2799         return updated;
2800 }
2801
2802
2803 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
2804                                    struct wpa_scan_results *scan_res)
2805 {
2806         struct wpa_bss *bss;
2807         int freq;
2808         u8 iface_addr[ETH_ALEN];
2809
2810         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2811
2812         if (wpa_s->global->p2p_disabled)
2813                 return;
2814
2815         wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
2816                    scan_res ? (int) scan_res->num : -1,
2817                    wpa_s->p2p_auto_join ? "auto_" : "");
2818
2819         if (scan_res)
2820                 wpas_p2p_scan_res_handler(wpa_s, scan_res);
2821
2822         if (wpa_s->p2p_auto_pd) {
2823                 int join = wpas_p2p_peer_go(wpa_s,
2824                                             wpa_s->pending_join_dev_addr);
2825                 if (join == 0 &&
2826                     wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
2827                         wpa_s->auto_pd_scan_retry++;
2828                         bss = wpa_bss_get_bssid(wpa_s,
2829                                                 wpa_s->pending_join_dev_addr);
2830                         if (bss) {
2831                                 freq = bss->freq;
2832                                 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
2833                                            "the peer " MACSTR " at %d MHz",
2834                                            wpa_s->auto_pd_scan_retry,
2835                                            MAC2STR(wpa_s->
2836                                                    pending_join_dev_addr),
2837                                            freq);
2838                                 wpas_p2p_join_scan_req(wpa_s, freq);
2839                                 return;
2840                         }
2841                 }
2842
2843                 if (join < 0)
2844                         join = 0;
2845
2846                 wpa_s->p2p_auto_pd = 0;
2847                 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
2848                 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
2849                            MAC2STR(wpa_s->pending_join_dev_addr), join);
2850                 if (p2p_prov_disc_req(wpa_s->global->p2p,
2851                                       wpa_s->pending_join_dev_addr,
2852                                       wpa_s->pending_pd_config_methods, join,
2853                                       0) < 0) {
2854                         wpa_s->p2p_auto_pd = 0;
2855                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2856                                 " p2p_dev_addr=" MACSTR " status=N/A",
2857                                 MAC2STR(wpa_s->pending_join_dev_addr));
2858                 }
2859                 return;
2860         }
2861
2862         if (wpa_s->p2p_auto_join) {
2863                 int join = wpas_p2p_peer_go(wpa_s,
2864                                             wpa_s->pending_join_dev_addr);
2865                 if (join < 0) {
2866                         wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
2867                                    "running a GO -> use GO Negotiation");
2868                         wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
2869                                          wpa_s->p2p_pin, wpa_s->p2p_wps_method,
2870                                          wpa_s->p2p_persistent_group, 0, 0, 0,
2871                                          wpa_s->p2p_go_intent,
2872                                          wpa_s->p2p_connect_freq,
2873                                          wpa_s->p2p_persistent_id,
2874                                          wpa_s->p2p_pd_before_go_neg);
2875                         return;
2876                 }
2877
2878                 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
2879                            "try to join the group", join ? "" :
2880                            " in older scan");
2881                 if (!join)
2882                         wpa_s->p2p_fallback_to_go_neg = 1;
2883         }
2884
2885         freq = p2p_get_oper_freq(wpa_s->global->p2p,
2886                                  wpa_s->pending_join_iface_addr);
2887         if (freq < 0 &&
2888             p2p_get_interface_addr(wpa_s->global->p2p,
2889                                    wpa_s->pending_join_dev_addr,
2890                                    iface_addr) == 0 &&
2891             os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
2892         {
2893                 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
2894                            "address for join from " MACSTR " to " MACSTR
2895                            " based on newly discovered P2P peer entry",
2896                            MAC2STR(wpa_s->pending_join_iface_addr),
2897                            MAC2STR(iface_addr));
2898                 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
2899                           ETH_ALEN);
2900
2901                 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2902                                          wpa_s->pending_join_iface_addr);
2903         }
2904         if (freq >= 0) {
2905                 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2906                            "from P2P peer table: %d MHz", freq);
2907         }
2908         bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
2909         if (bss) {
2910                 freq = bss->freq;
2911                 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2912                            "from BSS table: %d MHz", freq);
2913         }
2914         if (freq > 0) {
2915                 u16 method;
2916
2917                 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
2918                         wpa_msg(wpa_s->parent, MSG_INFO,
2919                                 P2P_EVENT_GROUP_FORMATION_FAILURE
2920                                 "reason=FREQ_CONFLICT");
2921                         return;
2922                 }
2923
2924                 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2925                            "prior to joining an existing group (GO " MACSTR
2926                            " freq=%u MHz)",
2927                            MAC2STR(wpa_s->pending_join_dev_addr), freq);
2928                 wpa_s->pending_pd_before_join = 1;
2929
2930                 switch (wpa_s->pending_join_wps_method) {
2931                 case WPS_PIN_DISPLAY:
2932                         method = WPS_CONFIG_KEYPAD;
2933                         break;
2934                 case WPS_PIN_KEYPAD:
2935                         method = WPS_CONFIG_DISPLAY;
2936                         break;
2937                 case WPS_PBC:
2938                         method = WPS_CONFIG_PUSHBUTTON;
2939                         break;
2940                 default:
2941                         method = 0;
2942                         break;
2943                 }
2944
2945                 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
2946                                                wpa_s->pending_join_dev_addr) ==
2947                      method)) {
2948                         /*
2949                          * We have already performed provision discovery for
2950                          * joining the group. Proceed directly to join
2951                          * operation without duplicated provision discovery. */
2952                         wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
2953                                    "with " MACSTR " already done - proceed to "
2954                                    "join",
2955                                    MAC2STR(wpa_s->pending_join_dev_addr));
2956                         wpa_s->pending_pd_before_join = 0;
2957                         goto start;
2958                 }
2959
2960                 if (p2p_prov_disc_req(wpa_s->global->p2p,
2961                                       wpa_s->pending_join_dev_addr, method, 1,
2962                                       freq) < 0) {
2963                         wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2964                                    "Discovery Request before joining an "
2965                                    "existing group");
2966                         wpa_s->pending_pd_before_join = 0;
2967                         goto start;
2968                 }
2969
2970                 /*
2971                  * Actual join operation will be started from the Action frame
2972                  * TX status callback (if no ACK is received) or when the
2973                  * Provision Discovery Response is received. Use a short
2974                  * timeout as a backup mechanism should the Provision Discovery
2975                  * Response be lost for any reason.
2976                  */
2977                 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
2978                                      NULL);
2979                 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
2980                                        wpa_s, NULL);
2981                 return;
2982         }
2983
2984         wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
2985         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2986         eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2987         wpas_p2p_check_join_scan_limit(wpa_s);
2988         return;
2989
2990 start:
2991         /* Start join operation immediately */
2992         wpas_p2p_join_start(wpa_s);
2993 }
2994
2995
2996 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
2997 {
2998         int ret;
2999         struct wpa_driver_scan_params params;
3000         struct wpabuf *wps_ie, *ies;
3001         size_t ielen;
3002         int freqs[2] = { 0, 0 };
3003
3004         os_memset(&params, 0, sizeof(params));
3005
3006         /* P2P Wildcard SSID */
3007         params.num_ssids = 1;
3008         params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3009         params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3010
3011         wpa_s->wps->dev.p2p = 1;
3012         wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3013                                         wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3014                                         NULL);
3015         if (wps_ie == NULL) {
3016                 wpas_p2p_scan_res_join(wpa_s, NULL);
3017                 return;
3018         }
3019
3020         ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3021         ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
3022         if (ies == NULL) {
3023                 wpabuf_free(wps_ie);
3024                 wpas_p2p_scan_res_join(wpa_s, NULL);
3025                 return;
3026         }
3027         wpabuf_put_buf(ies, wps_ie);
3028         wpabuf_free(wps_ie);
3029
3030         p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
3031
3032         params.p2p_probe = 1;
3033         params.extra_ies = wpabuf_head(ies);
3034         params.extra_ies_len = wpabuf_len(ies);
3035         if (freq > 0) {
3036                 freqs[0] = freq;
3037                 params.freqs = freqs;
3038         }
3039
3040         /*
3041          * Run a scan to update BSS table and start Provision Discovery once
3042          * the new scan results become available.
3043          */
3044         ret = wpa_drv_scan(wpa_s, &params);
3045         if (!ret)
3046                 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
3047
3048         wpabuf_free(ies);
3049
3050         if (ret) {
3051                 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3052                            "try again later");
3053                 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3054                 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3055                 wpas_p2p_check_join_scan_limit(wpa_s);
3056         }
3057 }
3058
3059
3060 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3061 {
3062         struct wpa_supplicant *wpa_s = eloop_ctx;
3063         wpas_p2p_join_scan_req(wpa_s, 0);
3064 }
3065
3066
3067 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
3068                          const u8 *dev_addr, enum p2p_wps_method wps_method,
3069                          int auto_join)
3070 {
3071         wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
3072                    MACSTR " dev " MACSTR ")%s",
3073                    MAC2STR(iface_addr), MAC2STR(dev_addr),
3074                    auto_join ? " (auto_join)" : "");
3075
3076         wpa_s->p2p_auto_pd = 0;
3077         wpa_s->p2p_auto_join = !!auto_join;
3078         os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3079         os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3080         wpa_s->pending_join_wps_method = wps_method;
3081
3082         /* Make sure we are not running find during connection establishment */
3083         wpas_p2p_stop_find(wpa_s);
3084
3085         wpa_s->p2p_join_scan_count = 0;
3086         wpas_p2p_join_scan(wpa_s, NULL);
3087         return 0;
3088 }
3089
3090
3091 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3092 {
3093         struct wpa_supplicant *group;
3094         struct p2p_go_neg_results res;
3095         struct wpa_bss *bss;
3096
3097         eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
3098         group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3099         if (group == NULL)
3100                 return -1;
3101         if (group != wpa_s) {
3102                 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3103                           sizeof(group->p2p_pin));
3104                 group->p2p_wps_method = wpa_s->p2p_wps_method;
3105         }
3106
3107         group->p2p_in_provisioning = 1;
3108         group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
3109
3110         os_memset(&res, 0, sizeof(res));
3111         os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3112                   ETH_ALEN);
3113         res.wps_method = wpa_s->pending_join_wps_method;
3114         bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3115         if (bss) {
3116                 res.freq = bss->freq;
3117                 res.ssid_len = bss->ssid_len;
3118                 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3119         }
3120
3121         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3122                 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3123                            "starting client");
3124                 wpa_drv_cancel_remain_on_channel(wpa_s);
3125                 wpa_s->off_channel_freq = 0;
3126                 wpa_s->roc_waiting_drv_freq = 0;
3127         }
3128         wpas_start_wps_enrollee(group, &res);
3129
3130         /*
3131          * Allow a longer timeout for join-a-running-group than normal 15
3132          * second group formation timeout since the GO may not have authorized
3133          * our connection yet.
3134          */
3135         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3136         eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3137                                wpa_s, NULL);
3138
3139         return 0;
3140 }
3141
3142
3143 /**
3144  * wpas_p2p_connect - Request P2P Group Formation to be started
3145  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3146  * @peer_addr: Address of the peer P2P Device
3147  * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3148  * @persistent_group: Whether to create a persistent group
3149  * @auto_join: Whether to select join vs. GO Negotiation automatically
3150  * @join: Whether to join an existing group (as a client) instead of starting
3151  *      Group Owner negotiation; @peer_addr is BSSID in that case
3152  * @auth: Whether to only authorize the connection instead of doing that and
3153  *      initiating Group Owner negotiation
3154  * @go_intent: GO Intent or -1 to use default
3155  * @freq: Frequency for the group or 0 for auto-selection
3156  * @persistent_id: Persistent group credentials to use for forcing GO
3157  *      parameters or -1 to generate new values (SSID/passphrase)
3158  * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3159  *      interoperability workaround when initiating group formation
3160  * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3161  *      failure, -2 on failure due to channel not currently available,
3162  *      -3 if forced channel is not supported
3163  */
3164 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3165                      const char *pin, enum p2p_wps_method wps_method,
3166                      int persistent_group, int auto_join, int join, int auth,
3167                      int go_intent, int freq, int persistent_id, int pd)
3168 {
3169         int force_freq = 0, oper_freq = 0;
3170         u8 bssid[ETH_ALEN];
3171         int ret = 0;
3172         enum wpa_driver_if_type iftype;
3173         const u8 *if_addr;
3174         struct wpa_ssid *ssid = NULL;
3175
3176         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3177                 return -1;
3178
3179         if (persistent_id >= 0) {
3180                 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3181                 if (ssid == NULL || ssid->disabled != 2 ||
3182                     ssid->mode != WPAS_MODE_P2P_GO)
3183                         return -1;
3184         }
3185
3186         if (go_intent < 0)
3187                 go_intent = wpa_s->conf->p2p_go_intent;
3188
3189         if (!auth)
3190                 wpa_s->p2p_long_listen = 0;
3191
3192         wpa_s->p2p_wps_method = wps_method;
3193         wpa_s->p2p_persistent_group = !!persistent_group;
3194         wpa_s->p2p_persistent_id = persistent_id;
3195         wpa_s->p2p_go_intent = go_intent;
3196         wpa_s->p2p_connect_freq = freq;
3197         wpa_s->p2p_fallback_to_go_neg = 0;
3198         wpa_s->p2p_pd_before_go_neg = !!pd;
3199
3200         if (pin)
3201                 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3202         else if (wps_method == WPS_PIN_DISPLAY) {
3203                 ret = wps_generate_pin();
3204                 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3205                             ret);
3206                 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3207                            wpa_s->p2p_pin);
3208         } else
3209                 wpa_s->p2p_pin[0] = '\0';
3210
3211         if (join || auto_join) {
3212                 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3213                 if (auth) {
3214                         wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3215                                    "connect a running group from " MACSTR,
3216                                    MAC2STR(peer_addr));
3217                         os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3218                         return ret;
3219                 }
3220                 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3221                 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3222                                            iface_addr) < 0) {
3223                         os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3224                         p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3225                                          dev_addr);
3226                 }
3227                 if (auto_join) {
3228                         os_get_time(&wpa_s->p2p_auto_started);
3229                         wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3230                                    "%ld.%06ld",
3231                                    wpa_s->p2p_auto_started.sec,
3232                                    wpa_s->p2p_auto_started.usec);
3233                 }
3234                 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3235                                   auto_join) < 0)
3236                         return -1;
3237                 return ret;
3238         }
3239
3240         if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3241             wpa_s->assoc_freq)
3242                 oper_freq = wpa_s->assoc_freq;
3243         else {
3244                 oper_freq = wpa_drv_shared_freq(wpa_s);
3245                 if (oper_freq < 0)
3246                         oper_freq = 0;
3247         }
3248
3249         if (freq > 0) {
3250                 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3251                         wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3252                                    "(%u MHz) is not supported for P2P uses",
3253                                    freq);
3254                         return -3;
3255                 }
3256
3257                 if (oper_freq > 0 && freq != oper_freq &&
3258                     !(wpa_s->drv_flags &
3259                       WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3260                         wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3261                                    "on %u MHz while connected on another "
3262                                    "channel (%u MHz)", freq, oper_freq);
3263                         return -2;
3264                 }
3265                 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3266                            "requested channel (%u MHz)", freq);
3267                 force_freq = freq;
3268         } else if (oper_freq > 0 &&
3269                    !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3270                 if (!(wpa_s->drv_flags &
3271                       WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3272                         wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3273                                    "while connected on non-P2P supported "
3274                                    "channel (%u MHz)", oper_freq);
3275                         return -2;
3276                 }
3277                 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3278                            "(%u MHz) not available for P2P - try to use "
3279                            "another channel", oper_freq);
3280                 force_freq = 0;
3281         } else if (oper_freq > 0) {
3282                 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3283                            "channel we are already using (%u MHz) on another "
3284                            "interface", oper_freq);
3285                 force_freq = oper_freq;
3286         }
3287
3288         wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3289
3290         if (wpa_s->create_p2p_iface) {
3291                 /* Prepare to add a new interface for the group */
3292                 iftype = WPA_IF_P2P_GROUP;
3293                 if (go_intent == 15)
3294                         iftype = WPA_IF_P2P_GO;
3295                 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3296                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3297                                    "interface for the group");
3298                         return -1;
3299                 }
3300
3301                 if_addr = wpa_s->pending_interface_addr;
3302         } else
3303                 if_addr = wpa_s->own_addr;
3304
3305         if (auth) {
3306                 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
3307                                          go_intent, if_addr,
3308                                          force_freq, persistent_group, ssid) <
3309                     0)
3310                         return -1;
3311                 return ret;
3312         }
3313
3314         if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3315                                   go_intent, if_addr, force_freq,
3316                                   persistent_group, ssid) < 0) {
3317                 if (wpa_s->create_p2p_iface)
3318                         wpas_p2p_remove_pending_group_interface(wpa_s);
3319                 return -1;
3320         }
3321         return ret;
3322 }
3323
3324
3325 /**
3326  * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3327  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3328  * @freq: Frequency of the channel in MHz
3329  * @duration: Duration of the stay on the channel in milliseconds
3330  *
3331  * This callback is called when the driver indicates that it has started the
3332  * requested remain-on-channel duration.
3333  */
3334 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3335                                    unsigned int freq, unsigned int duration)
3336 {
3337         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3338                 return;
3339         if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3340                 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3341                               wpa_s->pending_listen_duration);
3342                 wpa_s->pending_listen_freq = 0;
3343         }
3344 }
3345
3346
3347 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3348                                  unsigned int timeout)
3349 {
3350         /* Limit maximum Listen state time based on driver limitation. */
3351         if (timeout > wpa_s->max_remain_on_chan)
3352                 timeout = wpa_s->max_remain_on_chan;
3353
3354         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3355                 return wpa_drv_p2p_listen(wpa_s, timeout);
3356
3357         return p2p_listen(wpa_s->global->p2p, timeout);
3358 }
3359
3360
3361 /**
3362  * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3363  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3364  * @freq: Frequency of the channel in MHz
3365  *
3366  * This callback is called when the driver indicates that a remain-on-channel
3367  * operation has been completed, i.e., the duration on the requested channel
3368  * has timed out.
3369  */
3370 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3371                                           unsigned int freq)
3372 {
3373         wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3374                    "(p2p_long_listen=%d ms pending_action_tx=%p)",
3375                    wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
3376         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3377                 return;
3378         if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3379                 return; /* P2P module started a new operation */
3380         if (wpa_s->pending_action_tx)
3381                 return;
3382         if (wpa_s->p2p_long_listen > 0)
3383                 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3384         if (wpa_s->p2p_long_listen > 0) {
3385                 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3386                 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3387         }
3388 }
3389
3390
3391 /**
3392  * wpas_p2p_group_remove - Remove a P2P group
3393  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3394  * @ifname: Network interface name of the group interface or "*" to remove all
3395  *      groups
3396  * Returns: 0 on success, -1 on failure
3397  *
3398  * This function is used to remove a P2P group. This can be used to disconnect
3399  * from a group in which the local end is a P2P Client or to end a P2P Group in
3400  * case the local end is the Group Owner. If a virtual network interface was
3401  * created for this group, that interface will be removed. Otherwise, only the
3402  * configured P2P group network will be removed from the interface.
3403  */
3404 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3405 {
3406         struct wpa_global *global = wpa_s->global;
3407
3408         if (os_strcmp(ifname, "*") == 0) {
3409                 struct wpa_supplicant *prev;
3410                 wpa_s = global->ifaces;
3411                 while (wpa_s) {
3412                         prev = wpa_s;
3413                         wpa_s = wpa_s->next;
3414                         wpas_p2p_disconnect(prev);
3415                 }
3416                 return 0;
3417         }
3418
3419         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3420                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3421                         break;
3422         }
3423
3424         return wpas_p2p_disconnect(wpa_s);
3425 }
3426
3427
3428 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3429                                    struct p2p_go_neg_results *params,
3430                                    int freq, int ht40)
3431 {
3432         u8 bssid[ETH_ALEN];
3433         int res;
3434
3435         os_memset(params, 0, sizeof(*params));
3436         params->role_go = 1;
3437         params->ht40 = ht40;
3438         if (freq) {
3439                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3440                            "frequency %d MHz", freq);
3441                 params->freq = freq;
3442         } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3443                    wpa_s->conf->p2p_oper_channel >= 1 &&
3444                    wpa_s->conf->p2p_oper_channel <= 11) {
3445                 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3446                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3447                            "frequency %d MHz", params->freq);
3448         } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
3449                    wpa_s->conf->p2p_oper_reg_class == 124) {
3450                 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3451                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3452                            "frequency %d MHz", params->freq);
3453         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3454                    wpa_s->best_overall_freq > 0 &&
3455                    p2p_supported_freq(wpa_s->global->p2p,
3456                                       wpa_s->best_overall_freq)) {
3457                 params->freq = wpa_s->best_overall_freq;
3458                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3459                            "channel %d MHz", params->freq);
3460         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3461                    wpa_s->best_24_freq > 0 &&
3462                    p2p_supported_freq(wpa_s->global->p2p,
3463                                       wpa_s->best_24_freq)) {
3464                 params->freq = wpa_s->best_24_freq;
3465                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3466                            "channel %d MHz", params->freq);
3467         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3468                    wpa_s->best_5_freq > 0 &&
3469                    p2p_supported_freq(wpa_s->global->p2p,
3470                                       wpa_s->best_5_freq)) {
3471                 params->freq = wpa_s->best_5_freq;
3472                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3473                            "channel %d MHz", params->freq);
3474         } else {
3475                 int chan;
3476                 for (chan = 0; chan < 11; chan++) {
3477                         params->freq = 2412 + chan * 5;
3478                         if (!wpas_p2p_disallowed_freq(wpa_s->global,
3479                                                       params->freq))
3480                                 break;
3481                 }
3482                 if (chan == 11) {
3483                         wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3484                                    "allowed");
3485                         return -1;
3486                 }
3487                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3488                            "known)", params->freq);
3489         }
3490
3491         if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3492             wpa_s->assoc_freq && !freq) {
3493                 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3494                            "already using");
3495                 params->freq = wpa_s->assoc_freq;
3496         }
3497
3498         res = wpa_drv_shared_freq(wpa_s);
3499         if (res > 0 && !freq) {
3500                 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3501                            "already using on a shared interface");
3502                 params->freq = res;
3503         } else if (res > 0 && freq != res &&
3504                    !(wpa_s->drv_flags &
3505                      WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3506                 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3507                            "while connected on another channel (%u MHz)",
3508                            freq, res);
3509                 return -1;
3510         }
3511
3512         return 0;
3513 }
3514
3515
3516 static struct wpa_supplicant *
3517 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3518                          int go)
3519 {
3520         struct wpa_supplicant *group_wpa_s;
3521
3522         if (!wpas_p2p_create_iface(wpa_s))
3523                 return wpa_s;
3524
3525         if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3526                                          WPA_IF_P2P_CLIENT) < 0)
3527                 return NULL;
3528         group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3529         if (group_wpa_s == NULL) {
3530                 wpas_p2p_remove_pending_group_interface(wpa_s);
3531                 return NULL;
3532         }
3533
3534         return group_wpa_s;
3535 }
3536
3537
3538 /**
3539  * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3540  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3541  * @persistent_group: Whether to create a persistent group
3542  * @freq: Frequency for the group or 0 to indicate no hardcoding
3543  * Returns: 0 on success, -1 on failure
3544  *
3545  * This function creates a new P2P group with the local end as the Group Owner,
3546  * i.e., without using Group Owner Negotiation.
3547  */
3548 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3549                        int freq, int ht40)
3550 {
3551         struct p2p_go_neg_results params;
3552         unsigned int r;
3553
3554         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3555                 return -1;
3556
3557         /* Make sure we are not running find during connection establishment */
3558         wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
3559         wpas_p2p_stop_find(wpa_s);
3560
3561         if (freq == 2) {
3562                 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3563                            "band");
3564                 if (wpa_s->best_24_freq > 0 &&
3565                     p2p_supported_freq(wpa_s->global->p2p,
3566                                        wpa_s->best_24_freq)) {
3567                         freq = wpa_s->best_24_freq;
3568                         wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3569                                    "channel: %d MHz", freq);
3570                 } else {
3571                         os_get_random((u8 *) &r, sizeof(r));
3572                         freq = 2412 + (r % 3) * 25;
3573                         wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3574                                    "channel: %d MHz", freq);
3575                 }
3576         }
3577
3578         if (freq == 5) {
3579                 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3580                            "band");
3581                 if (wpa_s->best_5_freq > 0 &&
3582                     p2p_supported_freq(wpa_s->global->p2p,
3583                                        wpa_s->best_5_freq)) {
3584                         freq = wpa_s->best_5_freq;
3585                         wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3586                                    "channel: %d MHz", freq);
3587                 } else {
3588                         os_get_random((u8 *) &r, sizeof(r));
3589                         freq = 5180 + (r % 4) * 20;
3590                         if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3591                                 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3592                                            "5 GHz channel for P2P group");
3593                                 return -1;
3594                         }
3595                         wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3596                                    "channel: %d MHz", freq);
3597                 }
3598         }
3599
3600         if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3601                 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3602                            "(%u MHz) is not supported for P2P uses",
3603                            freq);
3604                 return -1;
3605         }
3606
3607         if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
3608                 return -1;
3609         if (params.freq &&
3610             !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3611                 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3612                            "(%u MHz) is not supported for P2P uses",
3613                            params.freq);
3614                 return -1;
3615         }
3616         p2p_go_params(wpa_s->global->p2p, &params);
3617         params.persistent_group = persistent_group;
3618
3619         wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3620         if (wpa_s == NULL)
3621                 return -1;
3622         wpas_start_wps_go(wpa_s, &params, 0);
3623
3624         return 0;
3625 }
3626
3627
3628 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3629                                  struct wpa_ssid *params, int addr_allocated)
3630 {
3631         struct wpa_ssid *ssid;
3632
3633         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3634         if (wpa_s == NULL)
3635                 return -1;
3636
3637         wpa_supplicant_ap_deinit(wpa_s);
3638
3639         ssid = wpa_config_add_network(wpa_s->conf);
3640         if (ssid == NULL)
3641                 return -1;
3642         wpa_config_set_network_defaults(ssid);
3643         ssid->temporary = 1;
3644         ssid->proto = WPA_PROTO_RSN;
3645         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3646         ssid->group_cipher = WPA_CIPHER_CCMP;
3647         ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3648         ssid->ssid = os_malloc(params->ssid_len);
3649         if (ssid->ssid == NULL) {
3650                 wpa_config_remove_network(wpa_s->conf, ssid->id);
3651                 return -1;
3652         }
3653         os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3654         ssid->ssid_len = params->ssid_len;
3655         ssid->p2p_group = 1;
3656         ssid->export_keys = 1;
3657         if (params->psk_set) {
3658                 os_memcpy(ssid->psk, params->psk, 32);
3659                 ssid->psk_set = 1;
3660         }
3661         if (params->passphrase)
3662                 ssid->passphrase = os_strdup(params->passphrase);
3663
3664         wpa_supplicant_select_network(wpa_s, ssid);
3665
3666         wpa_s->show_group_started = 1;
3667
3668         return 0;
3669 }
3670
3671
3672 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3673                                   struct wpa_ssid *ssid, int addr_allocated,
3674                                   int freq, int ht40)
3675 {
3676         struct p2p_go_neg_results params;
3677         int go = 0;
3678
3679         if (ssid->disabled != 2 || ssid->ssid == NULL)
3680                 return -1;
3681
3682         if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3683             go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3684                 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3685                            "already running");
3686                 return 0;
3687         }
3688
3689         /* Make sure we are not running find during connection establishment */
3690         wpas_p2p_stop_find(wpa_s);
3691
3692         wpa_s->p2p_fallback_to_go_neg = 0;
3693
3694         if (ssid->mode == WPAS_MODE_INFRA)
3695                 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3696
3697         if (ssid->mode != WPAS_MODE_P2P_GO)
3698                 return -1;
3699
3700         if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
3701                 return -1;
3702
3703         params.role_go = 1;
3704         if (ssid->passphrase == NULL ||
3705             os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3706                 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
3707                            "group");
3708                 return -1;
3709         }
3710         os_strlcpy(params.passphrase, ssid->passphrase,
3711                    sizeof(params.passphrase));
3712         os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
3713         params.ssid_len = ssid->ssid_len;
3714         params.persistent_group = 1;
3715
3716         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
3717         if (wpa_s == NULL)
3718                 return -1;
3719
3720         wpas_start_wps_go(wpa_s, &params, 0);
3721
3722         return 0;
3723 }
3724
3725
3726 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
3727                                struct wpabuf *proberesp_ies)
3728 {
3729         struct wpa_supplicant *wpa_s = ctx;
3730         if (wpa_s->ap_iface) {
3731                 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
3732                 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
3733                         wpabuf_free(beacon_ies);
3734                         wpabuf_free(proberesp_ies);
3735                         return;
3736                 }
3737                 if (beacon_ies) {
3738                         wpabuf_free(hapd->p2p_beacon_ie);
3739                         hapd->p2p_beacon_ie = beacon_ies;
3740                 }
3741                 wpabuf_free(hapd->p2p_probe_resp_ie);
3742                 hapd->p2p_probe_resp_ie = proberesp_ies;
3743         } else {
3744                 wpabuf_free(beacon_ies);
3745                 wpabuf_free(proberesp_ies);
3746         }
3747         wpa_supplicant_ap_update_beacon(wpa_s);
3748 }
3749
3750
3751 static void wpas_p2p_idle_update(void *ctx, int idle)
3752 {
3753         struct wpa_supplicant *wpa_s = ctx;
3754         if (!wpa_s->ap_iface)
3755                 return;
3756         wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
3757         if (idle)
3758                 wpas_p2p_set_group_idle_timeout(wpa_s);
3759         else
3760                 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3761 }
3762
3763
3764 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
3765                                        struct wpa_ssid *ssid)
3766 {
3767         struct p2p_group *group;
3768         struct p2p_group_config *cfg;
3769
3770         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3771                 return NULL;
3772         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3773                 return NULL;
3774
3775         cfg = os_zalloc(sizeof(*cfg));
3776         if (cfg == NULL)
3777                 return NULL;
3778
3779         if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
3780                 cfg->persistent_group = 2;
3781         else if (ssid->p2p_persistent_group)
3782                 cfg->persistent_group = 1;
3783         os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
3784         if (wpa_s->max_stations &&
3785             wpa_s->max_stations < wpa_s->conf->max_num_sta)
3786                 cfg->max_clients = wpa_s->max_stations;
3787         else
3788                 cfg->max_clients = wpa_s->conf->max_num_sta;
3789         os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
3790         cfg->ssid_len = ssid->ssid_len;
3791         cfg->cb_ctx = wpa_s;
3792         cfg->ie_update = wpas_p2p_ie_update;
3793         cfg->idle_update = wpas_p2p_idle_update;
3794
3795         group = p2p_group_init(wpa_s->global->p2p, cfg);
3796         if (group == NULL)
3797                 os_free(cfg);
3798         if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3799                 p2p_group_notif_formation_done(group);
3800         wpa_s->p2p_group = group;
3801         return group;
3802 }
3803
3804
3805 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3806                           int registrar)
3807 {
3808         struct wpa_ssid *ssid = wpa_s->current_ssid;
3809
3810         if (!wpa_s->p2p_in_provisioning) {
3811                 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
3812                            "provisioning not in progress");
3813                 return;
3814         }
3815
3816         if (ssid && ssid->mode == WPAS_MODE_INFRA) {
3817                 u8 go_dev_addr[ETH_ALEN];
3818                 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
3819                 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3820                                           ssid->ssid_len);
3821                 /* Clear any stored provisioning info */
3822                 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
3823         }
3824
3825         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
3826                              NULL);
3827         if (ssid && ssid->mode == WPAS_MODE_INFRA) {
3828                 /*
3829                  * Use a separate timeout for initial data connection to
3830                  * complete to allow the group to be removed automatically if
3831                  * something goes wrong in this step before the P2P group idle
3832                  * timeout mechanism is taken into use.
3833                  */
3834                 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
3835                                        wpas_p2p_group_formation_timeout,
3836                                        wpa_s->parent, NULL);
3837         }
3838         if (wpa_s->global->p2p)
3839                 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
3840         else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3841                 wpa_drv_wps_success_cb(wpa_s, peer_addr);
3842         wpas_group_formation_completed(wpa_s, 1);
3843 }
3844
3845
3846 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
3847                          struct wps_event_fail *fail)
3848 {
3849         if (!wpa_s->p2p_in_provisioning) {
3850                 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
3851                            "provisioning not in progress");
3852                 return;
3853         }
3854
3855         if (wpa_s->go_params) {
3856                 p2p_clear_provisioning_info(
3857                         wpa_s->global->p2p,
3858                         wpa_s->go_params->peer_device_addr);
3859         }
3860
3861         wpas_notify_p2p_wps_failed(wpa_s, fail);
3862 }
3863
3864
3865 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3866                        const char *config_method,
3867                        enum wpas_p2p_prov_disc_use use)
3868 {
3869         u16 config_methods;
3870
3871         wpa_s->p2p_fallback_to_go_neg = 0;
3872         wpa_s->pending_pd_use = NORMAL_PD;
3873         if (os_strncmp(config_method, "display", 7) == 0)
3874                 config_methods = WPS_CONFIG_DISPLAY;
3875         else if (os_strncmp(config_method, "keypad", 6) == 0)
3876                 config_methods = WPS_CONFIG_KEYPAD;
3877         else if (os_strncmp(config_method, "pbc", 3) == 0 ||
3878                  os_strncmp(config_method, "pushbutton", 10) == 0)
3879                 config_methods = WPS_CONFIG_PUSHBUTTON;
3880         else {
3881                 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
3882                 return -1;
3883         }
3884
3885         if (use == WPAS_P2P_PD_AUTO) {
3886                 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
3887                 wpa_s->pending_pd_config_methods = config_methods;
3888                 wpa_s->p2p_auto_pd = 1;
3889                 wpa_s->p2p_auto_join = 0;
3890                 wpa_s->pending_pd_before_join = 0;
3891                 wpa_s->auto_pd_scan_retry = 0;
3892                 wpas_p2p_stop_find(wpa_s);
3893                 wpa_s->p2p_join_scan_count = 0;
3894                 os_get_time(&wpa_s->p2p_auto_started);
3895                 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
3896                            wpa_s->p2p_auto_started.sec,
3897                            wpa_s->p2p_auto_started.usec);
3898                 wpas_p2p_join_scan(wpa_s, NULL);
3899                 return 0;
3900         }
3901
3902         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3903                 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
3904                                                  config_methods,
3905                                                  use == WPAS_P2P_PD_FOR_JOIN);
3906         }
3907
3908         if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
3909                 return -1;
3910
3911         return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
3912                                  config_methods, use == WPAS_P2P_PD_FOR_JOIN,
3913                                  0);
3914 }
3915
3916
3917 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
3918                               char *end)
3919 {
3920         return p2p_scan_result_text(ies, ies_len, buf, end);
3921 }
3922
3923
3924 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
3925 {
3926         if (!wpa_s->pending_action_tx)
3927                 return;
3928
3929         wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
3930                    "operation request");
3931         wpabuf_free(wpa_s->pending_action_tx);
3932         wpa_s->pending_action_tx = NULL;
3933 }
3934
3935
3936 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
3937                   enum p2p_discovery_type type,
3938                   unsigned int num_req_dev_types, const u8 *req_dev_types,
3939                   const u8 *dev_id)
3940 {
3941         wpas_p2p_clear_pending_action_tx(wpa_s);
3942         wpa_s->p2p_long_listen = 0;
3943
3944         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3945                 return wpa_drv_p2p_find(wpa_s, timeout, type);
3946
3947         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
3948             wpa_s->p2p_in_provisioning)
3949                 return -1;
3950
3951         wpa_supplicant_cancel_sched_scan(wpa_s);
3952
3953         return p2p_find(wpa_s->global->p2p, timeout, type,
3954                         num_req_dev_types, req_dev_types, dev_id);
3955 }
3956
3957
3958 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
3959 {
3960         wpas_p2p_clear_pending_action_tx(wpa_s);
3961         wpa_s->p2p_long_listen = 0;
3962         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3963         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3964         wpa_s->p2p_cb_on_scan_complete = 0;
3965
3966         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3967                 wpa_drv_p2p_stop_find(wpa_s);
3968                 return;
3969         }
3970
3971         if (wpa_s->global->p2p)
3972                 p2p_stop_find(wpa_s->global->p2p);
3973
3974         wpas_p2p_remove_pending_group_interface(wpa_s);
3975 }
3976
3977
3978 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3979 {
3980         struct wpa_supplicant *wpa_s = eloop_ctx;
3981         wpa_s->p2p_long_listen = 0;
3982 }
3983
3984
3985 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
3986 {
3987         int res;
3988
3989         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3990                 return -1;
3991
3992         wpa_supplicant_cancel_sched_scan(wpa_s);
3993         wpas_p2p_clear_pending_action_tx(wpa_s);
3994
3995         if (timeout == 0) {
3996                 /*
3997                  * This is a request for unlimited Listen state. However, at
3998                  * least for now, this is mapped to a Listen state for one
3999                  * hour.
4000                  */
4001                 timeout = 3600;
4002         }
4003         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4004         wpa_s->p2p_long_listen = 0;
4005
4006         /*
4007          * Stop previous find/listen operation to avoid trying to request a new
4008          * remain-on-channel operation while the driver is still running the
4009          * previous one.
4010          */
4011         if (wpa_s->global->p2p)
4012                 p2p_stop_find(wpa_s->global->p2p);
4013
4014         res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4015         if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4016                 wpa_s->p2p_long_listen = timeout * 1000;
4017                 eloop_register_timeout(timeout, 0,
4018                                        wpas_p2p_long_listen_timeout,
4019                                        wpa_s, NULL);
4020         }
4021
4022         return res;
4023 }
4024
4025
4026 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4027                           u8 *buf, size_t len, int p2p_group)
4028 {
4029         struct wpabuf *p2p_ie;
4030         int ret;
4031
4032         if (wpa_s->global->p2p_disabled)
4033                 return -1;
4034         if (wpa_s->global->p2p == NULL)
4035                 return -1;
4036         if (bss == NULL)
4037                 return -1;
4038
4039         p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4040         ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4041                                p2p_group, p2p_ie);
4042         wpabuf_free(p2p_ie);
4043
4044         return ret;
4045 }
4046
4047
4048 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
4049                           const u8 *dst, const u8 *bssid,
4050                           const u8 *ie, size_t ie_len, int ssi_signal)
4051 {
4052         if (wpa_s->global->p2p_disabled)
4053                 return 0;
4054         if (wpa_s->global->p2p == NULL)
4055                 return 0;
4056
4057         switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4058                                  ie, ie_len)) {
4059         case P2P_PREQ_NOT_P2P:
4060                 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4061                                  ssi_signal);
4062                 /* fall through */
4063         case P2P_PREQ_MALFORMED:
4064         case P2P_PREQ_NOT_LISTEN:
4065         case P2P_PREQ_NOT_PROCESSED:
4066         default: /* make gcc happy */
4067                 return 0;
4068         case P2P_PREQ_PROCESSED:
4069                 return 1;
4070         }
4071 }
4072
4073
4074 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4075                         const u8 *sa, const u8 *bssid,
4076                         u8 category, const u8 *data, size_t len, int freq)
4077 {
4078         if (wpa_s->global->p2p_disabled)
4079                 return;
4080         if (wpa_s->global->p2p == NULL)
4081                 return;
4082
4083         p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4084                       freq);
4085 }
4086
4087
4088 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4089 {
4090         if (wpa_s->global->p2p_disabled)
4091                 return;
4092         if (wpa_s->global->p2p == NULL)
4093                 return;
4094
4095         p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4096 }
4097
4098
4099 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4100 {
4101         p2p_group_deinit(wpa_s->p2p_group);
4102         wpa_s->p2p_group = NULL;
4103
4104         wpa_s->ap_configured_cb = NULL;
4105         wpa_s->ap_configured_cb_ctx = NULL;
4106         wpa_s->ap_configured_cb_data = NULL;
4107         wpa_s->connect_without_scan = NULL;
4108 }
4109
4110
4111 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4112 {
4113         wpa_s->p2p_long_listen = 0;
4114
4115         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4116                 return wpa_drv_p2p_reject(wpa_s, addr);
4117
4118         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4119                 return -1;
4120
4121         return p2p_reject(wpa_s->global->p2p, addr);
4122 }
4123
4124
4125 /* Invite to reinvoke a persistent group */
4126 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4127                     struct wpa_ssid *ssid, const u8 *go_dev_addr)
4128 {
4129         enum p2p_invite_role role;
4130         u8 *bssid = NULL;
4131
4132         if (ssid->mode == WPAS_MODE_P2P_GO) {
4133                 role = P2P_INVITE_ROLE_GO;
4134                 if (peer_addr == NULL) {
4135                         wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4136                                    "address in invitation command");
4137                         return -1;
4138                 }
4139                 if (wpas_p2p_create_iface(wpa_s)) {
4140                         if (wpas_p2p_add_group_interface(wpa_s,
4141                                                          WPA_IF_P2P_GO) < 0) {
4142                                 wpa_printf(MSG_ERROR, "P2P: Failed to "
4143                                            "allocate a new interface for the "
4144                                            "group");
4145                                 return -1;
4146                         }
4147                         bssid = wpa_s->pending_interface_addr;
4148                 } else
4149                         bssid = wpa_s->own_addr;
4150         } else {
4151                 role = P2P_INVITE_ROLE_CLIENT;
4152                 peer_addr = ssid->bssid;
4153         }
4154         wpa_s->pending_invite_ssid_id = ssid->id;
4155
4156         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4157                 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4158                                           ssid->ssid, ssid->ssid_len,
4159                                           go_dev_addr, 1);
4160
4161         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4162                 return -1;
4163
4164         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4165                           ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
4166 }
4167
4168
4169 /* Invite to join an active group */
4170 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4171                           const u8 *peer_addr, const u8 *go_dev_addr)
4172 {
4173         struct wpa_global *global = wpa_s->global;
4174         enum p2p_invite_role role;
4175         u8 *bssid = NULL;
4176         struct wpa_ssid *ssid;
4177         int persistent;
4178
4179         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4180                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4181                         break;
4182         }
4183         if (wpa_s == NULL) {
4184                 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4185                 return -1;
4186         }
4187
4188         ssid = wpa_s->current_ssid;
4189         if (ssid == NULL) {
4190                 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4191                            "invitation");
4192                 return -1;
4193         }
4194
4195         persistent = ssid->p2p_persistent_group &&
4196                 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4197                                         ssid->ssid, ssid->ssid_len);
4198
4199         if (ssid->mode == WPAS_MODE_P2P_GO) {
4200                 role = P2P_INVITE_ROLE_ACTIVE_GO;
4201                 bssid = wpa_s->own_addr;
4202                 if (go_dev_addr == NULL)
4203                         go_dev_addr = wpa_s->global->p2p_dev_addr;
4204         } else {
4205                 role = P2P_INVITE_ROLE_CLIENT;
4206                 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4207                         wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4208                                    "invite to current group");
4209                         return -1;
4210                 }
4211                 bssid = wpa_s->bssid;
4212                 if (go_dev_addr == NULL &&
4213                     !is_zero_ether_addr(wpa_s->go_dev_addr))
4214                         go_dev_addr = wpa_s->go_dev_addr;
4215         }
4216         wpa_s->parent->pending_invite_ssid_id = -1;
4217
4218         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4219                 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4220                                           ssid->ssid, ssid->ssid_len,
4221                                           go_dev_addr, persistent);
4222
4223         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4224                 return -1;
4225
4226         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4227                           ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
4228                           go_dev_addr, persistent);
4229 }
4230
4231
4232 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4233 {
4234         struct wpa_ssid *ssid = wpa_s->current_ssid;
4235         const char *ssid_txt;
4236         u8 go_dev_addr[ETH_ALEN];
4237         int network_id = -1;
4238         int persistent;
4239         int freq;
4240
4241         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4242                 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4243                                      wpa_s->parent, NULL);
4244         }
4245
4246         if (!wpa_s->show_group_started || !ssid)
4247                 return;
4248
4249         wpa_s->show_group_started = 0;
4250
4251         ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4252         os_memset(go_dev_addr, 0, ETH_ALEN);
4253         if (ssid->bssid_set)
4254                 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4255         persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4256                                                ssid->ssid_len);
4257         os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4258
4259         if (wpa_s->global->p2p_group_formation == wpa_s)
4260                 wpa_s->global->p2p_group_formation = NULL;
4261
4262         freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4263                 (int) wpa_s->assoc_freq;
4264         if (ssid->passphrase == NULL && ssid->psk_set) {
4265                 char psk[65];
4266                 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4267                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4268                         "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4269                         MACSTR "%s",
4270                         wpa_s->ifname, ssid_txt, freq, psk,
4271                         MAC2STR(go_dev_addr),
4272                         persistent ? " [PERSISTENT]" : "");
4273         } else {
4274                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4275                         "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4276                         "go_dev_addr=" MACSTR "%s",
4277                         wpa_s->ifname, ssid_txt, freq,
4278                         ssid->passphrase ? ssid->passphrase : "",
4279                         MAC2STR(go_dev_addr),
4280                         persistent ? " [PERSISTENT]" : "");
4281         }
4282
4283         if (persistent)
4284                 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4285                                                              ssid, go_dev_addr);
4286         if (network_id < 0)
4287                 network_id = ssid->id;
4288         wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
4289 }
4290
4291
4292 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4293                           u32 interval1, u32 duration2, u32 interval2)
4294 {
4295         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4296                 return -1;
4297         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4298                 return -1;
4299
4300         if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4301             wpa_s->current_ssid == NULL ||
4302             wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4303                 return -1;
4304
4305         return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4306                                 wpa_s->own_addr, wpa_s->assoc_freq,
4307                                 duration1, interval1, duration2, interval2);
4308 }
4309
4310
4311 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4312                         unsigned int interval)
4313 {
4314         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4315                 return -1;
4316
4317         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4318                 return -1;
4319
4320         return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4321 }
4322
4323
4324 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4325 {
4326         return wpa_s->current_ssid != NULL &&
4327                 wpa_s->current_ssid->p2p_group &&
4328                 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4329 }
4330
4331
4332 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4333 {
4334         struct wpa_supplicant *wpa_s = eloop_ctx;
4335
4336         if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
4337                 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4338                            "disabled");
4339                 return;
4340         }
4341
4342         wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4343                    "group");
4344         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
4345 }
4346
4347
4348 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4349 {
4350         int timeout;
4351
4352         if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4353                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4354
4355         if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4356                 return;
4357
4358         timeout = wpa_s->conf->p2p_group_idle;
4359         if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4360             (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4361             timeout = P2P_MAX_CLIENT_IDLE;
4362
4363         if (timeout == 0)
4364                 return;
4365
4366         if (timeout < 0) {
4367                 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4368                         timeout = 0; /* special client mode no-timeout */
4369                 else
4370                         return;
4371         }
4372
4373         if (wpa_s->p2p_in_provisioning) {
4374                 /*
4375                  * Use the normal group formation timeout during the
4376                  * provisioning phase to avoid terminating this process too
4377                  * early due to group idle timeout.
4378                  */
4379                 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4380                            "during provisioning");
4381                 return;
4382         }
4383
4384         if (wpa_s->show_group_started) {
4385                 /*
4386                  * Use the normal group formation timeout between the end of
4387                  * the provisioning phase and completion of 4-way handshake to
4388                  * avoid terminating this process too early due to group idle
4389                  * timeout.
4390                  */
4391                 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4392                            "while waiting for initial 4-way handshake to "
4393                            "complete");
4394                 return;
4395         }
4396
4397         wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
4398                    timeout);
4399         eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4400                                wpa_s, NULL);
4401 }
4402
4403
4404 void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4405                            u16 reason_code, const u8 *ie, size_t ie_len,
4406                            int locally_generated)
4407 {
4408         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4409                 return;
4410         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4411                 return;
4412
4413         if (!locally_generated)
4414                 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4415                                  ie_len);
4416
4417         if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4418             wpa_s->current_ssid &&
4419             wpa_s->current_ssid->p2p_group &&
4420             wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4421                 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4422                            "session is ending");
4423                 wpas_p2p_group_delete(wpa_s,
4424                                       P2P_GROUP_REMOVAL_GO_ENDING_SESSION);
4425         }
4426 }
4427
4428
4429 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4430                              u16 reason_code, const u8 *ie, size_t ie_len,
4431                              int locally_generated)
4432 {
4433         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4434                 return;
4435         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4436                 return;
4437
4438         if (!locally_generated)
4439                 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4440                                    ie_len);
4441 }
4442
4443
4444 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4445 {
4446         struct p2p_data *p2p = wpa_s->global->p2p;
4447
4448         if (p2p == NULL)
4449                 return;
4450
4451         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4452                 return;
4453
4454         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4455                 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4456
4457         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4458                 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4459
4460         if (wpa_s->wps &&
4461             (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4462                 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4463
4464         if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4465                 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4466
4467         if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4468                 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4469                 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4470                 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4471                 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4472         }
4473
4474         if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4475                 p2p_set_sec_dev_types(p2p,
4476                                       (void *) wpa_s->conf->sec_device_type,
4477                                       wpa_s->conf->num_sec_device_types);
4478
4479         if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4480                 int i;
4481                 p2p_remove_wps_vendor_extensions(p2p);
4482                 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4483                         if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4484                                 continue;
4485                         p2p_add_wps_vendor_extension(
4486                                 p2p, wpa_s->conf->wps_vendor_ext[i]);
4487                 }
4488         }
4489
4490         if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4491             wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4492                 char country[3];
4493                 country[0] = wpa_s->conf->country[0];
4494                 country[1] = wpa_s->conf->country[1];
4495                 country[2] = 0x04;
4496                 p2p_set_country(p2p, country);
4497         }
4498
4499         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4500                 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4501                                      wpa_s->conf->p2p_ssid_postfix ?
4502                                      os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4503                                      0);
4504         }
4505
4506         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4507                 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
4508
4509         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4510                 u8 reg_class, channel;
4511                 int ret;
4512                 unsigned int r;
4513                 if (wpa_s->conf->p2p_listen_reg_class &&
4514                     wpa_s->conf->p2p_listen_channel) {
4515                         reg_class = wpa_s->conf->p2p_listen_reg_class;
4516                         channel = wpa_s->conf->p2p_listen_channel;
4517                 } else {
4518                         reg_class = 81;
4519                         /*
4520                          * Pick one of the social channels randomly as the
4521                          * listen channel.
4522                          */
4523                         os_get_random((u8 *) &r, sizeof(r));
4524                         channel = 1 + (r % 3) * 5;
4525                 }
4526                 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4527                 if (ret)
4528                         wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4529                                    "failed: %d", ret);
4530         }
4531         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4532                 u8 op_reg_class, op_channel, cfg_op_channel;
4533                 int ret = 0;
4534                 unsigned int r;
4535                 if (wpa_s->conf->p2p_oper_reg_class &&
4536                     wpa_s->conf->p2p_oper_channel) {
4537                         op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4538                         op_channel = wpa_s->conf->p2p_oper_channel;
4539                         cfg_op_channel = 1;
4540                 } else {
4541                         op_reg_class = 81;
4542                         /*
4543                          * Use random operation channel from (1, 6, 11)
4544                          *if no other preference is indicated.
4545                          */
4546                         os_get_random((u8 *) &r, sizeof(r));
4547                         op_channel = 1 + (r % 3) * 5;
4548                         cfg_op_channel = 0;
4549                 }
4550                 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4551                                            cfg_op_channel);
4552                 if (ret)
4553                         wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4554                                    "failed: %d", ret);
4555         }
4556
4557         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4558                 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4559                                       wpa_s->conf->p2p_pref_chan) < 0) {
4560                         wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4561                                    "update failed");
4562                 }
4563         }
4564 }
4565
4566
4567 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4568                      int duration)
4569 {
4570         if (!wpa_s->ap_iface)
4571                 return -1;
4572         return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4573                                    duration);
4574 }
4575
4576
4577 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4578 {
4579         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4580                 return -1;
4581         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4582                 return -1;
4583
4584         wpa_s->global->cross_connection = enabled;
4585         p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4586
4587         if (!enabled) {
4588                 struct wpa_supplicant *iface;
4589
4590                 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4591                 {
4592                         if (iface->cross_connect_enabled == 0)
4593                                 continue;
4594
4595                         iface->cross_connect_enabled = 0;
4596                         iface->cross_connect_in_use = 0;
4597                         wpa_msg(iface->parent, MSG_INFO,
4598                                 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4599                                 iface->ifname, iface->cross_connect_uplink);
4600                 }
4601         }
4602
4603         return 0;
4604 }
4605
4606
4607 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4608 {
4609         struct wpa_supplicant *iface;
4610
4611         if (!uplink->global->cross_connection)
4612                 return;
4613
4614         for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4615                 if (!iface->cross_connect_enabled)
4616                         continue;
4617                 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4618                     0)
4619                         continue;
4620                 if (iface->ap_iface == NULL)
4621                         continue;
4622                 if (iface->cross_connect_in_use)
4623                         continue;
4624
4625                 iface->cross_connect_in_use = 1;
4626                 wpa_msg(iface->parent, MSG_INFO,
4627                         P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4628                         iface->ifname, iface->cross_connect_uplink);
4629         }
4630 }
4631
4632
4633 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4634 {
4635         struct wpa_supplicant *iface;
4636
4637         for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4638                 if (!iface->cross_connect_enabled)
4639                         continue;
4640                 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4641                     0)
4642                         continue;
4643                 if (!iface->cross_connect_in_use)
4644                         continue;
4645
4646                 wpa_msg(iface->parent, MSG_INFO,
4647                         P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4648                         iface->ifname, iface->cross_connect_uplink);
4649                 iface->cross_connect_in_use = 0;
4650         }
4651 }
4652
4653
4654 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4655 {
4656         if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4657             wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4658             wpa_s->cross_connect_disallowed)
4659                 wpas_p2p_disable_cross_connect(wpa_s);
4660         else
4661                 wpas_p2p_enable_cross_connect(wpa_s);
4662         if (!wpa_s->ap_iface &&
4663             eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4664                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4665 }
4666
4667
4668 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4669 {
4670         wpas_p2p_disable_cross_connect(wpa_s);
4671         if (!wpa_s->ap_iface &&
4672             !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
4673                                          wpa_s, NULL))
4674                 wpas_p2p_set_group_idle_timeout(wpa_s);
4675 }
4676
4677
4678 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
4679 {
4680         struct wpa_supplicant *iface;
4681
4682         if (!wpa_s->global->cross_connection)
4683                 return;
4684
4685         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4686                 if (iface == wpa_s)
4687                         continue;
4688                 if (iface->drv_flags &
4689                     WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
4690                         continue;
4691                 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
4692                         continue;
4693
4694                 wpa_s->cross_connect_enabled = 1;
4695                 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
4696                            sizeof(wpa_s->cross_connect_uplink));
4697                 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
4698                            "%s to %s whenever uplink is available",
4699                            wpa_s->ifname, wpa_s->cross_connect_uplink);
4700
4701                 if (iface->ap_iface || iface->current_ssid == NULL ||
4702                     iface->current_ssid->mode != WPAS_MODE_INFRA ||
4703                     iface->cross_connect_disallowed ||
4704                     iface->wpa_state != WPA_COMPLETED)
4705                         break;
4706
4707                 wpa_s->cross_connect_in_use = 1;
4708                 wpa_msg(wpa_s->parent, MSG_INFO,
4709                         P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4710                         wpa_s->ifname, wpa_s->cross_connect_uplink);
4711                 break;
4712         }
4713 }
4714
4715
4716 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
4717 {
4718         if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
4719             !wpa_s->p2p_in_provisioning)
4720                 return 0; /* not P2P client operation */
4721
4722         wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
4723                    "session overlap");
4724         if (wpa_s != wpa_s->parent)
4725                 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
4726
4727         if (wpa_s->global->p2p)
4728                 p2p_group_formation_failed(wpa_s->global->p2p);
4729
4730         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4731                              wpa_s->parent, NULL);
4732
4733         wpas_group_formation_completed(wpa_s, 0);
4734         return 1;
4735 }
4736
4737
4738 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
4739 {
4740         struct p2p_channels chan;
4741
4742         if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
4743                 return;
4744
4745         os_memset(&chan, 0, sizeof(chan));
4746         if (wpas_p2p_setup_channels(wpa_s, &chan)) {
4747                 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
4748                            "channel list");
4749                 return;
4750         }
4751
4752         p2p_update_channel_list(wpa_s->global->p2p, &chan);
4753 }
4754
4755
4756 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
4757 {
4758         struct wpa_global *global = wpa_s->global;
4759         int found = 0;
4760         const u8 *peer;
4761
4762         if (global->p2p == NULL)
4763                 return -1;
4764
4765         wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
4766
4767         if (wpa_s->pending_interface_name[0] &&
4768             !is_zero_ether_addr(wpa_s->pending_interface_addr))
4769                 found = 1;
4770
4771         peer = p2p_get_go_neg_peer(global->p2p);
4772         if (peer) {
4773                 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
4774                            MACSTR, MAC2STR(peer));
4775                 p2p_unauthorize(global->p2p, peer);
4776                 found = 1;
4777         }
4778
4779         wpas_p2p_stop_find(wpa_s);
4780
4781         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4782                 if (wpa_s == global->p2p_group_formation &&
4783                     (wpa_s->p2p_in_provisioning ||
4784                      wpa_s->parent->pending_interface_type ==
4785                      WPA_IF_P2P_CLIENT)) {
4786                         wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
4787                                    "formation found - cancelling",
4788                                    wpa_s->ifname);
4789                         found = 1;
4790                         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4791                                              wpa_s->parent, NULL);
4792                         wpas_p2p_group_delete(wpa_s,
4793                                               P2P_GROUP_REMOVAL_REQUESTED);
4794                         break;
4795                 }
4796         }
4797
4798         if (!found) {
4799                 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
4800                 return -1;
4801         }
4802
4803         return 0;
4804 }
4805
4806
4807 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
4808 {
4809         if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4810                 return;
4811
4812         wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
4813                    "being available anymore");
4814         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
4815 }
4816
4817
4818 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
4819                                    int freq_24, int freq_5, int freq_overall)
4820 {
4821         struct p2p_data *p2p = wpa_s->global->p2p;
4822         if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4823                 return;
4824         p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
4825 }
4826
4827
4828 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
4829 {
4830         u8 peer[ETH_ALEN];
4831         struct p2p_data *p2p = wpa_s->global->p2p;
4832
4833         if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4834                 return -1;
4835
4836         if (hwaddr_aton(addr, peer))
4837                 return -1;
4838
4839         return p2p_unauthorize(p2p, peer);
4840 }
4841
4842
4843 /**
4844  * wpas_p2p_disconnect - Disconnect from a P2P Group
4845  * @wpa_s: Pointer to wpa_supplicant data
4846  * Returns: 0 on success, -1 on failure
4847  *
4848  * This can be used to disconnect from a group in which the local end is a P2P
4849  * Client or to end a P2P Group in case the local end is the Group Owner. If a
4850  * virtual network interface was created for this group, that interface will be
4851  * removed. Otherwise, only the configured P2P group network will be removed
4852  * from the interface.
4853  */
4854 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
4855 {
4856
4857         if (wpa_s == NULL)
4858                 return -1;
4859
4860         return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED);
4861 }
4862
4863
4864 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
4865 {
4866         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4867                 return 0;
4868
4869         return p2p_in_progress(wpa_s->global->p2p);
4870 }
4871
4872
4873 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
4874                               struct wpa_ssid *ssid)
4875 {
4876         if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
4877             eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4878                                  wpa_s->parent, NULL) > 0) {
4879                 /**
4880                  * Remove the network by scheduling the group formation
4881                  * timeout to happen immediately. The teardown code
4882                  * needs to be scheduled to run asynch later so that we
4883                  * don't delete data from under ourselves unexpectedly.
4884                  * Calling wpas_p2p_group_formation_timeout directly
4885                  * causes a series of crashes in WPS failure scenarios.
4886                  */
4887                 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
4888                            "P2P group network getting removed");
4889                 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
4890                                        wpa_s->parent, NULL);
4891         }
4892 }
4893
4894
4895 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
4896                                           const u8 *addr, const u8 *ssid,
4897                                           size_t ssid_len)
4898 {
4899         struct wpa_ssid *s;
4900         size_t i;
4901
4902         for (s = wpa_s->conf->ssid; s; s = s->next) {
4903                 if (s->disabled != 2)
4904                         continue;
4905                 if (ssid &&
4906                     (ssid_len != s->ssid_len ||
4907                      os_memcmp(ssid, s->ssid, ssid_len) != 0))
4908                         continue;
4909                 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
4910                         return s; /* peer is GO in the persistent group */
4911                 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
4912                         continue;
4913                 for (i = 0; i < s->num_p2p_clients; i++) {
4914                         if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
4915                                       addr, ETH_ALEN) == 0)
4916                                 return s; /* peer is P2P client in persistent
4917                                            * group */
4918                 }
4919         }
4920
4921         return NULL;
4922 }
4923
4924
4925 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
4926                                        const u8 *addr)
4927 {
4928         if (addr == NULL)
4929                 return;
4930         wpas_p2p_add_persistent_group_client(wpa_s, addr);
4931 }
4932
4933
4934 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
4935                                         int group_added)
4936 {
4937         struct wpa_supplicant *group = wpa_s;
4938         eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
4939         if (wpa_s->global->p2p_group_formation)
4940                 group = wpa_s->global->p2p_group_formation;
4941         wpa_s = wpa_s->parent;
4942         offchannel_send_action_done(wpa_s);
4943         if (group_added)
4944                 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
4945         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
4946         wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
4947                          wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
4948                          0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
4949                          wpa_s->p2p_persistent_id,
4950                          wpa_s->p2p_pd_before_go_neg);
4951 }
4952
4953
4954 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
4955 {
4956         if (!wpa_s->p2p_fallback_to_go_neg ||
4957             wpa_s->p2p_in_provisioning <= 5)
4958                 return 0;
4959
4960         if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
4961                 return 0; /* peer operating as a GO */
4962
4963         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
4964                 "fallback to GO Negotiation");
4965         wpas_p2p_fallback_to_go_neg(wpa_s, 1);
4966
4967         return 1;
4968 }