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