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