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