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