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