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