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