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