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