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