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