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