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