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