P2P: Add test mode for SD to force fragmented response
[mech_eap.git] / wpa_supplicant / p2p_supplicant.c
1 /*
2  * wpa_supplicant - P2P
3  * Copyright (c) 2009-2010, Atheros Communications
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "eloop.h"
19 #include "common/ieee802_11_common.h"
20 #include "common/ieee802_11_defs.h"
21 #include "common/wpa_ctrl.h"
22 #include "wps/wps_i.h"
23 #include "p2p/p2p.h"
24 #include "ap/hostapd.h"
25 #include "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_REQUESTED_INFO_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_REQUESTED_INFO_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
1363                 if (wpa_s->force_long_sd) {
1364                         wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1365                                    "response");
1366                         wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1367                         wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1368                         goto done;
1369                 }
1370
1371                 switch (srv_proto) {
1372                 case P2P_SERV_ALL_SERVICES:
1373                         wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1374                                    "for all services");
1375                         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1376                             dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1377                                 wpa_printf(MSG_DEBUG, "P2P: No service "
1378                                            "discovery protocols available");
1379                                 wpas_sd_add_proto_not_avail(
1380                                         resp, P2P_SERV_ALL_SERVICES,
1381                                         srv_trans_id);
1382                                 break;
1383                         }
1384                         wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1385                         wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1386                         break;
1387                 case P2P_SERV_BONJOUR:
1388                         wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1389                                             pos, tlv_end - pos);
1390                         break;
1391                 case P2P_SERV_UPNP:
1392                         wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1393                                          pos, tlv_end - pos);
1394                         break;
1395                 default:
1396                         wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1397                                    "protocol %u", srv_proto);
1398                         wpas_sd_add_proto_not_avail(resp, srv_proto,
1399                                                     srv_trans_id);
1400                         break;
1401                 }
1402
1403                 pos = tlv_end;
1404         }
1405
1406 done:
1407         wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1408
1409         wpabuf_free(resp);
1410 }
1411
1412
1413 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1414                       const u8 *tlvs, size_t tlvs_len)
1415 {
1416         struct wpa_supplicant *wpa_s = ctx;
1417         const u8 *pos = tlvs;
1418         const u8 *end = tlvs + tlvs_len;
1419         const u8 *tlv_end;
1420         u16 slen;
1421         size_t buf_len;
1422         char *buf;
1423
1424         wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1425                     tlvs, tlvs_len);
1426         if (tlvs_len > 1500) {
1427                 /* TODO: better way for handling this */
1428                 wpa_msg_ctrl(wpa_s, MSG_INFO,
1429                              P2P_EVENT_SERV_DISC_RESP MACSTR
1430                              " %u <long response: %u bytes>",
1431                              MAC2STR(sa), update_indic,
1432                              (unsigned int) tlvs_len);
1433         } else {
1434                 buf_len = 2 * tlvs_len + 1;
1435                 buf = os_malloc(buf_len);
1436                 if (buf) {
1437                         wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1438                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1439                                      P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1440                                      MAC2STR(sa), update_indic, buf);
1441                         os_free(buf);
1442                 }
1443         }
1444
1445         while (pos < end) {
1446                 u8 srv_proto, srv_trans_id, status;
1447
1448                 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1449                 slen = WPA_GET_LE16(pos);
1450                 pos += 2;
1451                 if (pos + slen > end || slen < 3) {
1452                         wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1453                                    "length");
1454                         return;
1455                 }
1456                 tlv_end = pos + slen;
1457
1458                 srv_proto = *pos++;
1459                 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1460                            srv_proto);
1461                 srv_trans_id = *pos++;
1462                 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1463                            srv_trans_id);
1464                 status = *pos++;
1465                 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1466                            status);
1467
1468                 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1469                             pos, tlv_end - pos);
1470
1471                 pos = tlv_end;
1472         }
1473 }
1474
1475
1476 void * wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1477                            const struct wpabuf *tlvs)
1478 {
1479         return p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1480 }
1481
1482
1483 void * wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1484                                 u8 version, const char *query)
1485 {
1486         struct wpabuf *tlvs;
1487         void *ret;
1488
1489         tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1490         if (tlvs == NULL)
1491                 return NULL;
1492         wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1493         wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1494         wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1495         wpabuf_put_u8(tlvs, version);
1496         wpabuf_put_str(tlvs, query);
1497         ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1498         wpabuf_free(tlvs);
1499         return ret;
1500 }
1501
1502
1503 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, void *req)
1504 {
1505         return p2p_sd_cancel_request(wpa_s->global->p2p, req);
1506 }
1507
1508
1509 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1510                           const u8 *dst, u8 dialog_token,
1511                           const struct wpabuf *resp_tlvs)
1512 {
1513         p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1514                         resp_tlvs);
1515 }
1516
1517
1518 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1519 {
1520         p2p_sd_service_update(wpa_s->global->p2p);
1521 }
1522
1523
1524 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1525 {
1526         dl_list_del(&bsrv->list);
1527         wpabuf_free(bsrv->query);
1528         wpabuf_free(bsrv->resp);
1529         os_free(bsrv);
1530 }
1531
1532
1533 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1534 {
1535         dl_list_del(&usrv->list);
1536         os_free(usrv->service);
1537         os_free(usrv);
1538 }
1539
1540
1541 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1542 {
1543         struct p2p_srv_bonjour *bsrv, *bn;
1544         struct p2p_srv_upnp *usrv, *un;
1545
1546         dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1547                               struct p2p_srv_bonjour, list)
1548                 wpas_p2p_srv_bonjour_free(bsrv);
1549
1550         dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1551                               struct p2p_srv_upnp, list)
1552                 wpas_p2p_srv_upnp_free(usrv);
1553
1554         wpas_p2p_sd_service_update(wpa_s);
1555 }
1556
1557
1558 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1559                                  struct wpabuf *query, struct wpabuf *resp)
1560 {
1561         struct p2p_srv_bonjour *bsrv;
1562
1563         bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1564         if (bsrv) {
1565                 wpabuf_free(query);
1566                 wpabuf_free(bsrv->resp);
1567                 bsrv->resp = resp;
1568                 return 0;
1569         }
1570
1571         bsrv = os_zalloc(sizeof(*bsrv));
1572         if (bsrv == NULL)
1573                 return -1;
1574         bsrv->query = query;
1575         bsrv->resp = resp;
1576         dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1577
1578         wpas_p2p_sd_service_update(wpa_s);
1579         return 0;
1580 }
1581
1582
1583 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1584                                  const struct wpabuf *query)
1585 {
1586         struct p2p_srv_bonjour *bsrv;
1587
1588         bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1589         if (bsrv == NULL)
1590                 return -1;
1591         wpas_p2p_srv_bonjour_free(bsrv);
1592         wpas_p2p_sd_service_update(wpa_s);
1593         return 0;
1594 }
1595
1596
1597 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1598                               const char *service)
1599 {
1600         struct p2p_srv_upnp *usrv;
1601
1602         if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1603                 return 0; /* Already listed */
1604         usrv = os_zalloc(sizeof(*usrv));
1605         if (usrv == NULL)
1606                 return -1;
1607         usrv->version = version;
1608         usrv->service = os_strdup(service);
1609         if (usrv->service == NULL) {
1610                 os_free(usrv);
1611                 return -1;
1612         }
1613         dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1614
1615         wpas_p2p_sd_service_update(wpa_s);
1616         return 0;
1617 }
1618
1619
1620 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1621                               const char *service)
1622 {
1623         struct p2p_srv_upnp *usrv;
1624
1625         usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1626         if (usrv == NULL)
1627                 return -1;
1628         wpas_p2p_srv_upnp_free(usrv);
1629         wpas_p2p_sd_service_update(wpa_s);
1630         return 0;
1631 }
1632
1633
1634 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
1635                                          const u8 *peer, const char *params)
1636 {
1637         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
1638                 MAC2STR(peer), wps_generate_pin(), params);
1639 }
1640
1641
1642 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1643                                         const u8 *peer, const char *params)
1644 {
1645         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1646                 MAC2STR(peer), params);
1647 }
1648
1649
1650 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1651                         const u8 *dev_addr, const u8 *pri_dev_type,
1652                         const char *dev_name, u16 supp_config_methods,
1653                         u8 dev_capab, u8 group_capab)
1654 {
1655         struct wpa_supplicant *wpa_s = ctx;
1656         char devtype[WPS_DEV_TYPE_BUFSIZE];
1657         char params[200];
1658         u8 empty_dev_type[8];
1659
1660         if (pri_dev_type == NULL) {
1661                 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1662                 pri_dev_type = empty_dev_type;
1663         }
1664         os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1665                     " pri_dev_type=%s name='%s' config_methods=0x%x "
1666                     "dev_capab=0x%x group_capab=0x%x",
1667                     MAC2STR(dev_addr),
1668                     wps_dev_type_bin2str(pri_dev_type, devtype,
1669                                          sizeof(devtype)),
1670                     dev_name, supp_config_methods, dev_capab, group_capab);
1671         params[sizeof(params) - 1] = '\0';
1672
1673         if (config_methods & WPS_CONFIG_DISPLAY)
1674                 wpas_prov_disc_local_display(wpa_s, peer, params);
1675         else if (config_methods & WPS_CONFIG_KEYPAD)
1676                 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1677         else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1678                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1679                         "%s", MAC2STR(peer), params);
1680 }
1681
1682
1683 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1684 {
1685         struct wpa_supplicant *wpa_s = ctx;
1686         if (config_methods & WPS_CONFIG_DISPLAY)
1687                 wpas_prov_disc_local_keypad(wpa_s, peer, "");
1688         else if (config_methods & WPS_CONFIG_KEYPAD)
1689                 wpas_prov_disc_local_display(wpa_s, peer, "");
1690         else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1691                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR,
1692                         MAC2STR(peer));
1693 }
1694
1695
1696 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
1697                                   const u8 *go_dev_addr, const u8 *ssid,
1698                                   size_t ssid_len, int *go, u8 *group_bssid,
1699                                   int *force_freq, int persistent_group)
1700 {
1701         struct wpa_supplicant *wpa_s = ctx;
1702         struct wpa_ssid *s;
1703         u8 cur_bssid[ETH_ALEN];
1704         int res;
1705
1706         if (!persistent_group) {
1707                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1708                            " to join an active group", MAC2STR(sa));
1709                 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
1710                     os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN) ==
1711                     0) {
1712                         wpa_printf(MSG_DEBUG, "P2P: Accept previously "
1713                                    "authorized invitation");
1714                         goto accept_inv;
1715                 }
1716                 /*
1717                  * Do not accept the invitation automatically; notify user and
1718                  * request approval.
1719                  */
1720                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1721         }
1722
1723         if (!wpa_s->conf->persistent_reconnect)
1724                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1725
1726         for (s = wpa_s->conf->ssid; s; s = s->next) {
1727                 if (s->disabled == 2 &&
1728                     os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
1729                     s->ssid_len == ssid_len &&
1730                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
1731                         break;
1732         }
1733
1734         if (!s) {
1735                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1736                            " requested reinvocation of an unknown group",
1737                            MAC2STR(sa));
1738                 return P2P_SC_FAIL_UNKNOWN_GROUP;
1739         }
1740
1741         if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
1742                 *go = 1;
1743                 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1744                         wpa_printf(MSG_DEBUG, "P2P: The only available "
1745                                    "interface is already in use - reject "
1746                                    "invitation");
1747                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1748                 }
1749                 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
1750         } else if (s->mode == WPAS_MODE_P2P_GO) {
1751                 *go = 1;
1752                 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
1753                 {
1754                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
1755                                    "interface address for the group");
1756                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1757                 }
1758                 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
1759                           ETH_ALEN);
1760         }
1761
1762 accept_inv:
1763         if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
1764             wpa_s->assoc_freq) {
1765                 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1766                            "the channel we are already using");
1767                 *force_freq = wpa_s->assoc_freq;
1768         }
1769
1770         res = wpa_drv_shared_freq(wpa_s);
1771         if (res > 0) {
1772                 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1773                            "with the channel we are already using on a "
1774                            "shared interface");
1775                 *force_freq = res;
1776         }
1777
1778         return P2P_SC_SUCCESS;
1779 }
1780
1781
1782 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
1783                                      const u8 *ssid, size_t ssid_len,
1784                                      const u8 *go_dev_addr, u8 status,
1785                                      int op_freq)
1786 {
1787         struct wpa_supplicant *wpa_s = ctx;
1788         struct wpa_ssid *s;
1789
1790         for (s = wpa_s->conf->ssid; s; s = s->next) {
1791                 if (s->disabled == 2 &&
1792                     s->ssid_len == ssid_len &&
1793                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
1794                         break;
1795         }
1796
1797         if (status == P2P_SC_SUCCESS) {
1798                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
1799                            " was accepted; op_freq=%d MHz",
1800                            MAC2STR(sa), op_freq);
1801                 if (s) {
1802                         wpas_p2p_group_add_persistent(
1803                                 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
1804                 } else {
1805                         wpas_p2p_join(wpa_s, bssid, go_dev_addr,
1806                                       wpa_s->p2p_wps_method);
1807                 }
1808                 return;
1809         }
1810
1811         if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
1812                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
1813                            " was rejected (status %u)", MAC2STR(sa), status);
1814                 return;
1815         }
1816
1817         if (!s) {
1818                 if (bssid) {
1819                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
1820                                 "sa=" MACSTR " go_dev_addr=" MACSTR
1821                                 " bssid=" MACSTR " unknown-network",
1822                                 MAC2STR(sa), MAC2STR(go_dev_addr),
1823                                 MAC2STR(bssid));
1824                 } else {
1825                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
1826                                 "sa=" MACSTR " go_dev_addr=" MACSTR
1827                                 " unknown-network",
1828                                 MAC2STR(sa), MAC2STR(go_dev_addr));
1829                 }
1830                 return;
1831         }
1832
1833         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
1834                 " persistent=%d", MAC2STR(sa), s->id);
1835 }
1836
1837
1838 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
1839 {
1840         struct wpa_supplicant *wpa_s = ctx;
1841         struct wpa_ssid *ssid;
1842
1843         if (bssid) {
1844                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
1845                         "status=%d " MACSTR,
1846                         status, MAC2STR(bssid));
1847         } else {
1848                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
1849                         "status=%d ", status);
1850         }
1851
1852         if (wpa_s->pending_invite_ssid_id == -1)
1853                 return; /* Invitation to active group */
1854
1855         if (status != P2P_SC_SUCCESS) {
1856                 wpas_p2p_remove_pending_group_interface(wpa_s);
1857                 return;
1858         }
1859
1860         ssid = wpa_config_get_network(wpa_s->conf,
1861                                       wpa_s->pending_invite_ssid_id);
1862         if (ssid == NULL) {
1863                 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
1864                            "data matching with invitation");
1865                 return;
1866         }
1867
1868         wpas_p2p_group_add_persistent(wpa_s, ssid,
1869                                       ssid->mode == WPAS_MODE_P2P_GO, 0);
1870 }
1871
1872
1873 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
1874                                    struct p2p_config *p2p)
1875 {
1876         struct hostapd_hw_modes *modes;
1877         u16 num_modes, flags;
1878         int i, cla;
1879         int band24 = 0, band5_low = 0, band5_high = 0;
1880
1881         /* TODO: more detailed selection of channels within reg_class based on
1882          * driver capabilities */
1883
1884         modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes, &flags);
1885         if (modes == NULL) {
1886                 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
1887                            "of all supported channels; assume dualband "
1888                            "support");
1889                 band24 = band5_low = band5_high = 1;
1890         } else {
1891                 for (i = 0; i < num_modes; i++) {
1892                         struct hostapd_hw_modes *mode;
1893                         mode = &modes[i];
1894                         if (mode->mode == HOSTAPD_MODE_IEEE80211G) {
1895                                 band24 = 1;
1896                         } else if (mode->mode == HOSTAPD_MODE_IEEE80211A) {
1897                                 int j;
1898                                 for (j = 0; j < mode->num_channels; j++) {
1899                                         struct hostapd_channel_data *ch;
1900                                         ch = &mode->channels[j];
1901                                         if (ch->chan == 36)
1902                                                 band5_low = 1;
1903                                         else if (ch->chan == 157)
1904                                                 band5_high = 1;
1905                                 }
1906                         }
1907                 }
1908         }
1909
1910         cla = 0;
1911
1912         if (band24) {
1913                 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
1914                            "2.4 GHz band");
1915
1916                 /* Operating class 81 - 2.4 GHz band channels 1..13 */
1917                 p2p->channels.reg_class[cla].reg_class = 81;
1918                 p2p->channels.reg_class[cla].channels = 13;
1919                 for (i = 0; i < 13; i++)
1920                         p2p->channels.reg_class[cla].channel[i] = i + 1;
1921                 cla++;
1922
1923                 /* Operating class 82 - 2.4 GHz band channel 14 */
1924                 p2p->channels.reg_class[cla].reg_class = 82;
1925                 p2p->channels.reg_class[cla].channels = 1;
1926                 p2p->channels.reg_class[cla].channel[0] = 14;
1927                 cla++;
1928
1929 #if 0
1930                 /* Operating class 83 - 2.4 GHz band channels 1..9; 40 MHz */
1931                 p2p->channels.reg_class[cla].reg_class = 83;
1932                 p2p->channels.reg_class[cla].channels = 9;
1933                 for (i = 0; i < 9; i++)
1934                         p2p->channels.reg_class[cla].channel[i] = i + 1;
1935                 cla++;
1936
1937                 /* Operating class 84 - 2.4 GHz band channels 5..13; 40 MHz */
1938                 p2p->channels.reg_class[cla].reg_class = 84;
1939                 p2p->channels.reg_class[cla].channels = 9;
1940                 for (i = 0; i < 9; i++)
1941                         p2p->channels.reg_class[cla].channel[i] = i + 5;
1942                 cla++;
1943 #endif
1944         }
1945
1946         if (band5_low) {
1947                 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
1948                            "lower 5 GHz band");
1949
1950                 /* Operating class 115 - 5 GHz, channels 36-48 */
1951                 p2p->channels.reg_class[cla].reg_class = 115;
1952                 p2p->channels.reg_class[cla].channels = 4;
1953                 p2p->channels.reg_class[cla].channel[0] = 36;
1954                 p2p->channels.reg_class[cla].channel[1] = 40;
1955                 p2p->channels.reg_class[cla].channel[2] = 44;
1956                 p2p->channels.reg_class[cla].channel[3] = 48;
1957                 cla++;
1958
1959 #if 0
1960                 /* Operating class 116 - 5 GHz, channels 36,44; 40 MHz */
1961                 p2p->channels.reg_class[cla].reg_class = 116;
1962                 p2p->channels.reg_class[cla].channels = 2;
1963                 p2p->channels.reg_class[cla].channel[0] = 36;
1964                 p2p->channels.reg_class[cla].channel[1] = 44;
1965                 cla++;
1966
1967                 /* Operating class 117 - 5 GHz, channels 40,48; 40 MHz */
1968                 p2p->channels.reg_class[cla].reg_class = 117;
1969                 p2p->channels.reg_class[cla].channels = 2;
1970                 p2p->channels.reg_class[cla].channel[0] = 40;
1971                 p2p->channels.reg_class[cla].channel[1] = 48;
1972                 cla++;
1973 #endif
1974         }
1975
1976         if (band5_high) {
1977                 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
1978                            "higher 5 GHz band");
1979
1980                 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
1981                 p2p->channels.reg_class[cla].reg_class = 124;
1982                 p2p->channels.reg_class[cla].channels = 4;
1983                 p2p->channels.reg_class[cla].channel[0] = 149;
1984                 p2p->channels.reg_class[cla].channel[1] = 153;
1985                 p2p->channels.reg_class[cla].channel[2] = 157;
1986                 p2p->channels.reg_class[cla].channel[3] = 161;
1987                 cla++;
1988
1989 #if 0
1990                 /* Operating class 126 - 5 GHz, channels 149,157; 40 MHz */
1991                 p2p->channels.reg_class[cla].reg_class = 126;
1992                 p2p->channels.reg_class[cla].channels = 2;
1993                 p2p->channels.reg_class[cla].channel[0] = 149;
1994                 p2p->channels.reg_class[cla].channel[1] = 157;
1995                 cla++;
1996
1997                 /* Operating class 127 - 5 GHz, channels 153,161; 40 MHz */
1998                 p2p->channels.reg_class[cla].reg_class = 127;
1999                 p2p->channels.reg_class[cla].channels = 2;
2000                 p2p->channels.reg_class[cla].channel[0] = 153;
2001                 p2p->channels.reg_class[cla].channel[1] = 161;
2002                 cla++;
2003 #endif
2004         }
2005
2006         p2p->channels.reg_classes = cla;
2007
2008         if (modes)
2009                 ieee80211_sta_free_hw_features(modes, num_modes);
2010
2011         return 0;
2012 }
2013
2014
2015 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2016                         size_t buf_len)
2017 {
2018         struct wpa_supplicant *wpa_s = ctx;
2019
2020         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2021                 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2022                         break;
2023         }
2024         if (wpa_s == NULL)
2025                 return -1;
2026
2027         return wpa_drv_get_noa(wpa_s, buf, buf_len);
2028 }
2029
2030
2031 /**
2032  * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2033  * @global: Pointer to global data from wpa_supplicant_init()
2034  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2035  * Returns: 0 on success, -1 on failure
2036  */
2037 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2038 {
2039         struct p2p_config p2p;
2040         unsigned int r;
2041         int i;
2042
2043         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2044                 return 0;
2045
2046 #ifdef CONFIG_CLIENT_MLME
2047         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) {
2048                 wpa_s->mlme.public_action_cb = p2p_rx_action_mlme;
2049                 wpa_s->mlme.public_action_cb_ctx = wpa_s;
2050         }
2051 #endif /* CONFIG_CLIENT_MLME */
2052
2053         if (wpa_drv_disable_11b_rates(wpa_s, 1) < 0) {
2054                 wpa_printf(MSG_DEBUG, "P2P: Failed to disable 11b rates");
2055                 /* Continue anyway; this is not really a fatal error */
2056         }
2057
2058         if (global->p2p)
2059                 return 0;
2060
2061         os_memset(&p2p, 0, sizeof(p2p));
2062         p2p.msg_ctx = wpa_s;
2063         p2p.cb_ctx = wpa_s;
2064         p2p.p2p_scan = wpas_p2p_scan;
2065         p2p.send_action = wpas_send_action;
2066         p2p.send_action_done = wpas_send_action_done;
2067         p2p.go_neg_completed = wpas_go_neg_completed;
2068         p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2069         p2p.dev_found = wpas_dev_found;
2070         p2p.start_listen = wpas_start_listen;
2071         p2p.stop_listen = wpas_stop_listen;
2072         p2p.send_probe_resp = wpas_send_probe_resp;
2073         p2p.sd_request = wpas_sd_request;
2074         p2p.sd_response = wpas_sd_response;
2075         p2p.prov_disc_req = wpas_prov_disc_req;
2076         p2p.prov_disc_resp = wpas_prov_disc_resp;
2077         p2p.invitation_process = wpas_invitation_process;
2078         p2p.invitation_received = wpas_invitation_received;
2079         p2p.invitation_result = wpas_invitation_result;
2080         p2p.get_noa = wpas_get_noa;
2081
2082         os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2083         os_memcpy(p2p.dev_addr, wpa_s->own_addr, ETH_ALEN);
2084         p2p.dev_name = wpa_s->conf->device_name;
2085
2086         if (wpa_s->conf->p2p_listen_reg_class &&
2087             wpa_s->conf->p2p_listen_channel) {
2088                 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2089                 p2p.channel = wpa_s->conf->p2p_listen_channel;
2090         } else {
2091                 p2p.reg_class = 81;
2092                 /*
2093                  * Pick one of the social channels randomly as the listen
2094                  * channel.
2095                  */
2096                 os_get_random((u8 *) &r, sizeof(r));
2097                 p2p.channel = 1 + (r % 3) * 5;
2098         }
2099
2100         if (wpa_s->conf->p2p_oper_reg_class &&
2101             wpa_s->conf->p2p_oper_channel) {
2102                 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2103                 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2104         } else {
2105                 p2p.op_reg_class = 81;
2106                 /*
2107                  * For initial tests, pick the operation channel randomly.
2108                  * TODO: Use scan results (etc.) to select the best channel.
2109                  */
2110                 p2p.op_channel = 1 + r % 11;
2111         }
2112         wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d  "
2113                    "Own preferred operation channel: %d",
2114                    p2p.channel, p2p.op_channel);
2115         if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2116                 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2117                 p2p.country[2] = 0x04;
2118         } else
2119                 os_memcpy(p2p.country, "US\x04", 3);
2120
2121         if (wpas_p2p_setup_channels(wpa_s, &p2p)) {
2122                 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2123                            "channel list");
2124                 return -1;
2125         }
2126
2127         if (wpa_s->conf->device_type &&
2128             wps_dev_type_str2bin(wpa_s->conf->device_type, p2p.pri_dev_type) <
2129             0) {
2130                 wpa_printf(MSG_ERROR, "P2P: Invalid device_type");
2131                 return -1;
2132         }
2133
2134         for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
2135                 if (wpa_s->conf->sec_device_type[i] == NULL)
2136                         continue;
2137                 if (wps_dev_type_str2bin(
2138                             wpa_s->conf->sec_device_type[i],
2139                             p2p.sec_dev_type[p2p.num_sec_dev_types]) < 0) {
2140                         wpa_printf(MSG_ERROR, "P2P: Invalid sec_device_type");
2141                         return -1;
2142                 }
2143                 p2p.num_sec_dev_types++;
2144                 if (p2p.num_sec_dev_types == P2P_SEC_DEVICE_TYPES)
2145                         break;
2146         }
2147
2148         p2p.concurrent_operations = !!(wpa_s->drv_flags &
2149                                        WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2150
2151         p2p.max_peers = 100;
2152
2153         if (wpa_s->conf->p2p_ssid_postfix) {
2154                 p2p.ssid_postfix_len =
2155                         os_strlen(wpa_s->conf->p2p_ssid_postfix);
2156                 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2157                         p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2158                 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2159                           p2p.ssid_postfix_len);
2160         }
2161
2162         global->p2p = p2p_init(&p2p);
2163         if (global->p2p == NULL)
2164                 return -1;
2165
2166         return 0;
2167 }
2168
2169
2170 /**
2171  * wpas_p2p_deinit - Deinitialize per-interface P2P data
2172  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2173  *
2174  * This function deinitialize per-interface P2P data.
2175  */
2176 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2177 {
2178         if (wpa_s->driver && wpa_s->drv_priv)
2179                 wpa_drv_probe_req_report(wpa_s, 0);
2180         os_free(wpa_s->go_params);
2181         wpa_s->go_params = NULL;
2182         wpabuf_free(wpa_s->pending_action_tx);
2183         wpa_s->pending_action_tx = NULL;
2184         eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
2185         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2186         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2187         wpa_s->p2p_long_listen = 0;
2188         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2189         wpas_p2p_remove_pending_group_interface(wpa_s);
2190
2191         /* TODO: remove group interface from the driver if this wpa_s instance
2192          * is on top of a P2P group interface */
2193 }
2194
2195
2196 /**
2197  * wpas_p2p_deinit_global - Deinitialize global P2P module
2198  * @global: Pointer to global data from wpa_supplicant_init()
2199  *
2200  * This function deinitializes the global (per device) P2P module.
2201  */
2202 void wpas_p2p_deinit_global(struct wpa_global *global)
2203 {
2204         struct wpa_supplicant *wpa_s, *tmp;
2205         char *ifname;
2206
2207         if (global->p2p == NULL)
2208                 return;
2209
2210         /* Remove remaining P2P group interfaces */
2211         wpa_s = global->ifaces;
2212         if (wpa_s)
2213                 wpas_p2p_service_flush(wpa_s);
2214         while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2215                 wpa_s = wpa_s->next;
2216         while (wpa_s) {
2217                 enum wpa_driver_if_type type;
2218                 tmp = global->ifaces;
2219                 while (tmp &&
2220                        (tmp == wpa_s ||
2221                         tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2222                         tmp = tmp->next;
2223                 }
2224                 if (tmp == NULL)
2225                         break;
2226                 ifname = os_strdup(tmp->ifname);
2227                 type = wpas_p2p_if_type(tmp->p2p_group_interface);
2228                 wpa_supplicant_remove_iface(global, tmp);
2229                 if (ifname)
2230                         wpa_drv_if_remove(wpa_s, type, ifname);
2231                 os_free(ifname);
2232         }
2233
2234         p2p_deinit(global->p2p);
2235         global->p2p = NULL;
2236 }
2237
2238
2239 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2240 {
2241         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
2242                 return 1; /* P2P group requires a new interface in every case
2243                            */
2244         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2245                 return 0; /* driver does not support concurrent operations */
2246         if (wpa_s->global->ifaces->next)
2247                 return 1; /* more that one interface already in use */
2248         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2249                 return 1; /* this interface is already in use */
2250         return 0;
2251 }
2252
2253
2254 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2255                                  const u8 *peer_addr,
2256                                  enum p2p_wps_method wps_method,
2257                                  int go_intent, const u8 *own_interface_addr,
2258                                  unsigned int force_freq, int persistent_group)
2259 {
2260         return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2261                            go_intent, own_interface_addr, force_freq,
2262                            persistent_group);
2263 }
2264
2265
2266 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2267                                 const u8 *peer_addr,
2268                                 enum p2p_wps_method wps_method,
2269                                 int go_intent, const u8 *own_interface_addr,
2270                                 unsigned int force_freq, int persistent_group)
2271 {
2272         return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2273                              go_intent, own_interface_addr, force_freq,
2274                              persistent_group);
2275 }
2276
2277
2278 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
2279                                    struct wpa_scan_results *scan_res)
2280 {
2281         struct wpa_bss *bss;
2282
2283         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2284
2285         if (wpa_s->global->p2p_disabled)
2286                 return;
2287
2288         wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for join",
2289                    scan_res ? (int) scan_res->num : -1);
2290
2291         if (scan_res)
2292                 wpas_p2p_scan_res_handler(wpa_s, scan_res);
2293
2294         bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
2295         if (bss) {
2296                 u16 method;
2297
2298                 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2299                            "prior to joining an existing group (GO " MACSTR
2300                            " freq=%u MHz)",
2301                            MAC2STR(wpa_s->pending_join_dev_addr), bss->freq);
2302                 wpa_s->pending_pd_before_join = 1;
2303
2304                 switch (wpa_s->pending_join_wps_method) {
2305                 case WPS_PIN_LABEL:
2306                 case WPS_PIN_DISPLAY:
2307                         method = WPS_CONFIG_KEYPAD;
2308                         break;
2309                 case WPS_PIN_KEYPAD:
2310                         method = WPS_CONFIG_DISPLAY;
2311                         break;
2312                 case WPS_PBC:
2313                         method = WPS_CONFIG_PUSHBUTTON;
2314                         break;
2315                 default:
2316                         method = 0;
2317                         break;
2318                 }
2319
2320                 if (p2p_prov_disc_req(wpa_s->global->p2p,
2321                                       wpa_s->pending_join_dev_addr, method, 1)
2322                     < 0) {
2323                         wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2324                                    "Discovery Request before joining an "
2325                                    "existing group");
2326                         wpa_s->pending_pd_before_join = 0;
2327                         goto start;
2328                 }
2329
2330                 /*
2331                  * Actual join operation will be started from the Action frame
2332                  * TX status callback.
2333                  */
2334                 return;
2335         }
2336
2337         wpa_printf(MSG_DEBUG, "P2P: Target BSS/GO not yet in BSS table - "
2338                    "cannot send Provision Discovery Request");
2339
2340 start:
2341         /* Start join operation immediately */
2342         wpas_p2p_join_start(wpa_s);
2343 }
2344
2345
2346 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
2347 {
2348         struct wpa_supplicant *wpa_s = eloop_ctx;
2349         int ret;
2350         struct wpa_driver_scan_params params;
2351         struct wpabuf *wps_ie, *ies;
2352
2353         os_memset(&params, 0, sizeof(params));
2354
2355         /* P2P Wildcard SSID */
2356         params.num_ssids = 1;
2357         params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
2358         params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
2359
2360         wpa_s->wps->dev.p2p = 1;
2361         wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
2362                                         WPS_REQ_ENROLLEE);
2363         if (wps_ie == NULL) {
2364                 wpas_p2p_scan_res_join(wpa_s, NULL);
2365                 return;
2366         }
2367
2368         ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
2369         if (ies == NULL) {
2370                 wpabuf_free(wps_ie);
2371                 wpas_p2p_scan_res_join(wpa_s, NULL);
2372                 return;
2373         }
2374         wpabuf_put_buf(ies, wps_ie);
2375         wpabuf_free(wps_ie);
2376
2377         p2p_scan_ie(wpa_s->global->p2p, ies);
2378
2379         params.extra_ies = wpabuf_head(ies);
2380         params.extra_ies_len = wpabuf_len(ies);
2381
2382         /*
2383          * Run a scan to update BSS table and start Provision Discovery once
2384          * the new scan results become available.
2385          */
2386         wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
2387         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
2388                 ret = ieee80211_sta_req_scan(wpa_s, &params);
2389         else
2390                 ret = wpa_drv_scan(wpa_s, &params);
2391
2392         wpabuf_free(ies);
2393
2394         if (ret) {
2395                 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
2396                            "try again later");
2397                 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2398                 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2399         }
2400 }
2401
2402
2403 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
2404                          const u8 *dev_addr, enum p2p_wps_method wps_method)
2405 {
2406         wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
2407                    MACSTR " dev " MACSTR ")",
2408                    MAC2STR(iface_addr), MAC2STR(dev_addr));
2409
2410         os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
2411         os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
2412         wpa_s->pending_join_wps_method = wps_method;
2413
2414         /* Make sure we are not running find during connection establishment */
2415         wpas_p2p_stop_find(wpa_s);
2416
2417         wpas_p2p_join_scan(wpa_s, NULL);
2418         return 0;
2419 }
2420
2421
2422 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
2423 {
2424         struct wpa_supplicant *group;
2425         struct p2p_go_neg_results res;
2426
2427         group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
2428         if (group == NULL)
2429                 return -1;
2430         if (group != wpa_s) {
2431                 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
2432                           sizeof(group->p2p_pin));
2433                 group->p2p_wps_method = wpa_s->p2p_wps_method;
2434         }
2435
2436         group->p2p_in_provisioning = 1;
2437
2438         os_memset(&res, 0, sizeof(res));
2439         os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
2440                   ETH_ALEN);
2441         res.wps_method = wpa_s->pending_join_wps_method;
2442         wpas_start_wps_enrollee(group, &res);
2443
2444         return 0;
2445 }
2446
2447
2448 /**
2449  * wpas_p2p_connect - Request P2P Group Formation to be started
2450  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2451  * @peer_addr: Address of the peer P2P Device
2452  * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2453  * @persistent_group: Whether to create a persistent group
2454  * @join: Whether to join an existing group (as a client) instead of starting
2455  *      Group Owner negotiation; @peer_addr is BSSID in that case
2456  * @auth: Whether to only authorize the connection instead of doing that and
2457  *      initiating Group Owner negotiation
2458  * @go_intent: GO Intent or -1 to use default
2459  * @freq: Frequency for the group or 0 for auto-selection
2460  * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on failure
2461  */
2462 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2463                      const char *pin, enum p2p_wps_method wps_method,
2464                      int persistent_group, int join, int auth, int go_intent,
2465                      int freq)
2466 {
2467         int force_freq = 0;
2468         u8 bssid[ETH_ALEN];
2469         int ret = 0;
2470         enum wpa_driver_if_type iftype;
2471
2472         if (go_intent < 0)
2473                 go_intent = wpa_s->conf->p2p_go_intent;
2474
2475         if (!auth)
2476                 wpa_s->p2p_long_listen = 0;
2477
2478         wpa_s->p2p_wps_method = wps_method;
2479
2480         if (pin)
2481                 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
2482         else if (wps_method == WPS_PIN_DISPLAY) {
2483                 ret = wps_generate_pin();
2484                 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
2485                             ret);
2486                 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
2487                            wpa_s->p2p_pin);
2488         } else
2489                 wpa_s->p2p_pin[0] = '\0';
2490
2491         if (join) {
2492                 u8 iface_addr[ETH_ALEN];
2493                 if (auth) {
2494                         wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
2495                                    "connect a running group from " MACSTR,
2496                                    MAC2STR(peer_addr));
2497                         os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
2498                         return ret;
2499                 }
2500                 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
2501                                            iface_addr) < 0)
2502                         os_memcpy(iface_addr, peer_addr, ETH_ALEN);
2503                 if (wpas_p2p_join(wpa_s, iface_addr, peer_addr, wps_method) <
2504                     0)
2505                         return -1;
2506                 return ret;
2507         }
2508
2509         if (freq > 0)
2510                 force_freq = freq;
2511         else if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2512                  wpa_s->assoc_freq)
2513                 force_freq = wpa_s->assoc_freq;
2514         else {
2515                 force_freq = wpa_drv_shared_freq(wpa_s);
2516                 if (force_freq < 0)
2517                         force_freq = 0;
2518         }
2519
2520         if (force_freq > 0) {
2521                 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2522                            "channel we are already using (%u MHz) on another "
2523                            "interface", force_freq);
2524         }
2525
2526         wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
2527
2528         if (!wpa_s->create_p2p_iface) {
2529                 if (auth) {
2530                         if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2531                                                  go_intent, wpa_s->own_addr,
2532                                                  force_freq, persistent_group)
2533                             < 0)
2534                                 return -1;
2535                         return ret;
2536                 }
2537                 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
2538                                           go_intent, wpa_s->own_addr,
2539                                           force_freq, persistent_group) < 0)
2540                         return -1;
2541                 return ret;
2542         }
2543
2544         /* Prepare to add a new interface for the group */
2545         iftype = WPA_IF_P2P_GROUP;
2546         if (join)
2547                 iftype = WPA_IF_P2P_CLIENT;
2548         else if (go_intent == 15)
2549                 iftype = WPA_IF_P2P_GO;
2550         if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
2551                 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2552                            "interface for the group");
2553                 return -1;
2554         }
2555
2556         if (auth) {
2557                 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2558                                          go_intent,
2559                                          wpa_s->pending_interface_addr,
2560                                          force_freq, persistent_group) < 0)
2561                         return -1;
2562                 return ret;
2563         }
2564         if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, go_intent,
2565                                   wpa_s->pending_interface_addr,
2566                                   force_freq, persistent_group) < 0) {
2567                 wpas_p2p_remove_pending_group_interface(wpa_s);
2568                 return -1;
2569         }
2570         return ret;
2571 }
2572
2573
2574 /**
2575  * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
2576  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2577  * @freq: Frequency of the channel in MHz
2578  * @duration: Duration of the stay on the channel in milliseconds
2579  *
2580  * This callback is called when the driver indicates that it has started the
2581  * requested remain-on-channel duration.
2582  */
2583 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
2584                                    unsigned int freq, unsigned int duration)
2585 {
2586         wpa_s->roc_waiting_drv_freq = 0;
2587         wpa_s->off_channel_freq = freq;
2588         wpas_send_action_cb(wpa_s, NULL);
2589         if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
2590                 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
2591                               wpa_s->pending_listen_duration);
2592                 wpa_s->pending_listen_freq = 0;
2593         }
2594 }
2595
2596
2597 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
2598                                  unsigned int timeout)
2599 {
2600         /* Limit maximum Listen state time based on driver limitation. */
2601         if (timeout > wpa_s->max_remain_on_chan)
2602                 timeout = wpa_s->max_remain_on_chan;
2603
2604         return p2p_listen(wpa_s->global->p2p, timeout);
2605 }
2606
2607
2608 /**
2609  * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
2610  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2611  * @freq: Frequency of the channel in MHz
2612  *
2613  * This callback is called when the driver indicates that a remain-on-channel
2614  * operation has been completed, i.e., the duration on the requested channel
2615  * has timed out.
2616  */
2617 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
2618                                           unsigned int freq)
2619 {
2620         wpa_s->off_channel_freq = 0;
2621         if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
2622                 return; /* P2P module started a new operation */
2623         if (wpa_s->pending_action_tx)
2624                 return;
2625         if (wpa_s->p2p_long_listen > 0)
2626                 wpa_s->p2p_long_listen -= 5;
2627         if (wpa_s->p2p_long_listen > 0) {
2628                 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
2629                 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen * 1000);
2630         }
2631 }
2632
2633
2634 /**
2635  * wpas_p2p_group_remove - Remove a P2P group
2636  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2637  * @ifname: Network interface name of the group interface or "*" to remove all
2638  *      groups
2639  * Returns: 0 on success, -1 on failure
2640  *
2641  * This function is used to remove a P2P group. This can be used to disconnect
2642  * from a group in which the local end is a P2P Client or to end a P2P Group in
2643  * case the local end is the Group Owner. If a virtual network interface was
2644  * created for this group, that interface will be removed. Otherwise, only the
2645  * configured P2P group network will be removed from the interface.
2646  */
2647 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
2648 {
2649         struct wpa_global *global = wpa_s->global;
2650
2651         if (os_strcmp(ifname, "*") == 0) {
2652                 struct wpa_supplicant *prev;
2653                 wpa_s = global->ifaces;
2654                 while (wpa_s) {
2655                         prev = wpa_s;
2656                         wpa_s = wpa_s->next;
2657                         wpas_p2p_group_delete(prev);
2658                 }
2659                 return 0;
2660         }
2661
2662         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2663                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2664                         break;
2665         }
2666
2667         if (wpa_s == NULL)
2668                 return -1;
2669
2670         wpas_p2p_group_delete(wpa_s);
2671
2672         return 0;
2673 }
2674
2675
2676 static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
2677                                     struct p2p_go_neg_results *params,
2678                                     int freq)
2679 {
2680         u8 bssid[ETH_ALEN];
2681         int res;
2682
2683         os_memset(params, 0, sizeof(*params));
2684         params->role_go = 1;
2685         params->freq = 2412;
2686         if (freq)
2687                 params->freq = freq;
2688         else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
2689                  wpa_s->conf->p2p_oper_channel >= 1 &&
2690                  wpa_s->conf->p2p_oper_channel <= 11)
2691                 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
2692         else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
2693                  wpa_s->conf->p2p_oper_reg_class == 118)
2694                 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
2695         if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2696             wpa_s->assoc_freq && !freq) {
2697                 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
2698                            "already using");
2699                 params->freq = wpa_s->assoc_freq;
2700         }
2701
2702         res = wpa_drv_shared_freq(wpa_s);
2703         if (res > 0 && !freq) {
2704                 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
2705                            "already using on a shared interface");
2706                 params->freq = res;
2707         }
2708 }
2709
2710
2711 static struct wpa_supplicant *
2712 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
2713                          int go)
2714 {
2715         struct wpa_supplicant *group_wpa_s;
2716
2717         if (!wpas_p2p_create_iface(wpa_s))
2718                 return wpa_s;
2719
2720         if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2721                                          WPA_IF_P2P_CLIENT) < 0)
2722                 return NULL;
2723         group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
2724         if (group_wpa_s == NULL) {
2725                 wpas_p2p_remove_pending_group_interface(wpa_s);
2726                 return NULL;
2727         }
2728
2729         return group_wpa_s;
2730 }
2731
2732
2733 /**
2734  * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
2735  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2736  * @persistent_group: Whether to create a persistent group
2737  * @freq: Frequency for the group or 0 to indicate no hardcoding
2738  * Returns: 0 on success, -1 on failure
2739  *
2740  * This function creates a new P2P group with the local end as the Group Owner,
2741  * i.e., without using Group Owner Negotiation.
2742  */
2743 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
2744                        int freq)
2745 {
2746         struct p2p_go_neg_results params;
2747
2748         wpas_p2p_init_go_params(wpa_s, &params, freq);
2749         p2p_go_params(wpa_s->global->p2p, &params);
2750         params.persistent_group = persistent_group;
2751
2752         wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
2753         if (wpa_s == NULL)
2754                 return -1;
2755         wpas_start_wps_go(wpa_s, &params, 0);
2756
2757         return 0;
2758 }
2759
2760
2761 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
2762                                  struct wpa_ssid *params, int addr_allocated)
2763 {
2764         struct wpa_ssid *ssid;
2765
2766         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
2767         if (wpa_s == NULL)
2768                 return -1;
2769
2770         wpa_supplicant_ap_deinit(wpa_s);
2771
2772         ssid = wpa_config_add_network(wpa_s->conf);
2773         if (ssid == NULL)
2774                 return -1;
2775         wpas_notify_network_added(wpa_s, ssid);
2776         wpa_config_set_network_defaults(ssid);
2777         ssid->temporary = 1;
2778         ssid->proto = WPA_PROTO_RSN;
2779         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
2780         ssid->group_cipher = WPA_CIPHER_CCMP;
2781         ssid->key_mgmt = WPA_KEY_MGMT_PSK;
2782         ssid->ssid = os_malloc(params->ssid_len);
2783         if (ssid->ssid == NULL) {
2784                 wpas_notify_network_removed(wpa_s, ssid);
2785                 wpa_config_remove_network(wpa_s->conf, ssid->id);
2786                 return -1;
2787         }
2788         os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2789         ssid->ssid_len = params->ssid_len;
2790         ssid->p2p_group = 1;
2791         if (params->psk_set) {
2792                 os_memcpy(ssid->psk, params->psk, 32);
2793                 ssid->psk_set = 1;
2794         }
2795         if (params->passphrase)
2796                 ssid->passphrase = os_strdup(params->passphrase);
2797
2798         wpa_supplicant_select_network(wpa_s, ssid);
2799
2800         wpa_s->show_group_started = 1;
2801
2802         return 0;
2803 }
2804
2805
2806 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
2807                                   struct wpa_ssid *ssid, int addr_allocated,
2808                                   int freq)
2809 {
2810         struct p2p_go_neg_results params;
2811
2812         if (ssid->disabled != 2 || ssid->ssid == NULL)
2813                 return -1;
2814
2815         wpa_s->p2p_long_listen = 0;
2816         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2817
2818         if (ssid->mode == WPAS_MODE_INFRA)
2819                 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
2820
2821         if (ssid->mode != WPAS_MODE_P2P_GO)
2822                 return -1;
2823
2824         wpas_p2p_init_go_params(wpa_s, &params, freq);
2825
2826         params.role_go = 1;
2827         if (ssid->passphrase == NULL ||
2828             os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
2829                 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
2830                            "group");
2831                 return -1;
2832         }
2833         os_strlcpy(params.passphrase, ssid->passphrase,
2834                    sizeof(params.passphrase));
2835         os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
2836         params.ssid_len = ssid->ssid_len;
2837         params.persistent_group = 1;
2838
2839         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
2840         if (wpa_s == NULL)
2841                 return -1;
2842
2843         wpas_start_wps_go(wpa_s, &params, 0);
2844
2845         return 0;
2846 }
2847
2848
2849 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
2850                                struct wpabuf *proberesp_ies)
2851 {
2852         struct wpa_supplicant *wpa_s = ctx;
2853         if (wpa_s->ap_iface) {
2854                 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
2855                 if (beacon_ies) {
2856                         wpabuf_free(hapd->p2p_beacon_ie);
2857                         hapd->p2p_beacon_ie = beacon_ies;
2858                 }
2859                 wpabuf_free(hapd->p2p_probe_resp_ie);
2860                 hapd->p2p_probe_resp_ie = proberesp_ies;
2861         } else {
2862                 wpabuf_free(beacon_ies);
2863                 wpabuf_free(proberesp_ies);
2864         }
2865         wpa_supplicant_ap_update_beacon(wpa_s);
2866 }
2867
2868
2869 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
2870                                        int persistent_group,
2871                                        int group_formation)
2872 {
2873         struct p2p_group *group;
2874         struct p2p_group_config *cfg;
2875
2876         cfg = os_zalloc(sizeof(*cfg));
2877         if (cfg == NULL)
2878                 return NULL;
2879
2880         cfg->persistent_group = persistent_group;
2881         os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
2882         cfg->cb_ctx = wpa_s;
2883         cfg->ie_update = wpas_p2p_ie_update;
2884
2885         group = p2p_group_init(wpa_s->global->p2p, cfg);
2886         if (group == NULL)
2887                 os_free(cfg);
2888         if (!group_formation)
2889                 p2p_group_notif_formation_done(group);
2890         wpa_s->p2p_group = group;
2891         return group;
2892 }
2893
2894
2895 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2896                           int registrar)
2897 {
2898         if (!wpa_s->p2p_in_provisioning) {
2899                 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
2900                            "provisioning not in progress");
2901                 return;
2902         }
2903
2904         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
2905                              NULL);
2906         if (wpa_s->global->p2p)
2907                 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
2908         wpas_group_formation_completed(wpa_s, 1);
2909 }
2910
2911
2912 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2913                        const char *config_method)
2914 {
2915         u16 config_methods;
2916
2917         if (os_strcmp(config_method, "display") == 0)
2918                 config_methods = WPS_CONFIG_DISPLAY;
2919         else if (os_strcmp(config_method, "keypad") == 0)
2920                 config_methods = WPS_CONFIG_KEYPAD;
2921         else if (os_strcmp(config_method, "pbc") == 0 ||
2922                  os_strcmp(config_method, "pushbutton") == 0)
2923                 config_methods = WPS_CONFIG_PUSHBUTTON;
2924         else
2925                 return -1;
2926
2927         if (wpa_s->global->p2p == NULL)
2928                 return -1;
2929
2930         return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
2931                                  config_methods, 0);
2932 }
2933
2934
2935 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
2936                               char *end)
2937 {
2938         return p2p_scan_result_text(ies, ies_len, buf, end);
2939 }
2940
2941
2942 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
2943                   enum p2p_discovery_type type)
2944 {
2945         wpa_s->p2p_long_listen = 0;
2946
2947         return p2p_find(wpa_s->global->p2p, timeout, type);
2948 }
2949
2950
2951 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
2952 {
2953         wpa_s->p2p_long_listen = 0;
2954         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2955
2956         p2p_stop_find(wpa_s->global->p2p);
2957
2958         wpas_p2p_remove_pending_group_interface(wpa_s);
2959 }
2960
2961
2962 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
2963 {
2964         struct wpa_supplicant *wpa_s = eloop_ctx;
2965         wpa_s->p2p_long_listen = 0;
2966 }
2967
2968
2969 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
2970 {
2971         int res;
2972
2973         if (timeout == 0) {
2974                 /*
2975                  * This is a request for unlimited Listen state. However, at
2976                  * least for now, this is mapped to a Listen state for one
2977                  * hour.
2978                  */
2979                 timeout = 3600;
2980         }
2981         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2982         wpa_s->p2p_long_listen = 0;
2983
2984         res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
2985         if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
2986                 wpa_s->p2p_long_listen = timeout;
2987                 eloop_register_timeout(timeout, 0,
2988                                        wpas_p2p_long_listen_timeout,
2989                                        wpa_s, NULL);
2990         }
2991
2992         return res;
2993 }
2994
2995
2996 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, const u8 *bssid,
2997                           u8 *buf, size_t len, int p2p_group)
2998 {
2999         if (wpa_s->global->p2p_disabled)
3000                 return -1;
3001         if (wpa_s->global->p2p == NULL)
3002                 return -1;
3003
3004         return p2p_assoc_req_ie(wpa_s->global->p2p, bssid, buf, len,
3005                                 p2p_group);
3006 }
3007
3008
3009 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
3010                           const u8 *ie, size_t ie_len)
3011 {
3012         if (wpa_s->global->p2p_disabled)
3013                 return 0;
3014         if (wpa_s->global->p2p == NULL)
3015                 return 0;
3016
3017         return p2p_probe_req_rx(wpa_s->global->p2p, addr, ie, ie_len);
3018 }
3019
3020
3021 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
3022                         const u8 *sa, const u8 *bssid,
3023                         u8 category, const u8 *data, size_t len, int freq)
3024 {
3025         if (wpa_s->global->p2p_disabled)
3026                 return;
3027         if (wpa_s->global->p2p == NULL)
3028                 return;
3029
3030         p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
3031                       freq);
3032 }
3033
3034
3035 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
3036 {
3037         if (wpa_s->global->p2p_disabled)
3038                 return;
3039         if (wpa_s->global->p2p == NULL)
3040                 return;
3041
3042         p2p_scan_ie(wpa_s->global->p2p, ies);
3043 }
3044
3045
3046 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
3047 {
3048         p2p_group_deinit(wpa_s->p2p_group);
3049         wpa_s->p2p_group = NULL;
3050 }
3051
3052
3053 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
3054 {
3055         wpa_s->p2p_long_listen = 0;
3056
3057         return p2p_reject(wpa_s->global->p2p, addr);
3058 }
3059
3060
3061 /* Invite to reinvoke a persistent group */
3062 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3063                     struct wpa_ssid *ssid, const u8 *go_dev_addr)
3064 {
3065         enum p2p_invite_role role;
3066         u8 *bssid = NULL;
3067
3068         if (ssid->mode == WPAS_MODE_P2P_GO) {
3069                 role = P2P_INVITE_ROLE_GO;
3070                 if (peer_addr == NULL) {
3071                         wpa_printf(MSG_DEBUG, "P2P: Missing peer "
3072                                    "address in invitation command");
3073                         return -1;
3074                 }
3075                 if (wpas_p2p_create_iface(wpa_s)) {
3076                         if (wpas_p2p_add_group_interface(wpa_s,
3077                                                          WPA_IF_P2P_GO) < 0) {
3078                                 wpa_printf(MSG_ERROR, "P2P: Failed to "
3079                                            "allocate a new interface for the "
3080                                            "group");
3081                                 return -1;
3082                         }
3083                         bssid = wpa_s->pending_interface_addr;
3084                 } else
3085                         bssid = wpa_s->own_addr;
3086         } else {
3087                 role = P2P_INVITE_ROLE_CLIENT;
3088                 peer_addr = ssid->bssid;
3089         }
3090         wpa_s->pending_invite_ssid_id = ssid->id;
3091
3092         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3093                           ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
3094 }
3095
3096
3097 /* Invite to join an active group */
3098 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
3099                           const u8 *peer_addr, const u8 *go_dev_addr)
3100 {
3101         struct wpa_global *global = wpa_s->global;
3102         enum p2p_invite_role role;
3103         u8 *bssid = NULL;
3104         struct wpa_ssid *ssid;
3105
3106         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3107                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3108                         break;
3109         }
3110         if (wpa_s == NULL) {
3111                 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
3112                 return -1;
3113         }
3114
3115         ssid = wpa_s->current_ssid;
3116         if (ssid == NULL) {
3117                 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
3118                            "invitation");
3119                 return -1;
3120         }
3121
3122         if (ssid->mode == WPAS_MODE_P2P_GO) {
3123                 role = P2P_INVITE_ROLE_ACTIVE_GO;
3124                 bssid = wpa_s->own_addr;
3125                 if (go_dev_addr == NULL)
3126                         go_dev_addr = wpa_s->parent->own_addr;
3127         } else {
3128                 role = P2P_INVITE_ROLE_CLIENT;
3129                 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
3130                         wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
3131                                    "invite to current group");
3132                         return -1;
3133                 }
3134                 bssid = wpa_s->bssid;
3135                 if (go_dev_addr == NULL &&
3136                     !is_zero_ether_addr(wpa_s->go_dev_addr))
3137                         go_dev_addr = wpa_s->go_dev_addr;
3138         }
3139         wpa_s->pending_invite_ssid_id = -1;
3140
3141         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3142                           ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 0);
3143 }
3144
3145
3146 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
3147 {
3148         struct wpa_ssid *ssid = wpa_s->current_ssid;
3149         const char *ssid_txt;
3150         u8 go_dev_addr[ETH_ALEN];
3151         int persistent;
3152
3153         if (!wpa_s->show_group_started || !ssid)
3154                 return;
3155
3156         wpa_s->show_group_started = 0;
3157
3158         ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
3159         os_memset(go_dev_addr, 0, ETH_ALEN);
3160         if (ssid->bssid_set)
3161                 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
3162         persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3163                                                ssid->ssid_len);
3164         os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
3165
3166         if (ssid->passphrase == NULL && ssid->psk_set) {
3167                 char psk[65];
3168                 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
3169                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3170                         "%s client ssid=\"%s\" psk=%s go_dev_addr=" MACSTR
3171                         "%s",
3172                         wpa_s->ifname, ssid_txt, psk, MAC2STR(go_dev_addr),
3173                         persistent ? " [PERSISTENT]" : "");
3174         } else {
3175                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3176                         "%s client ssid=\"%s\" passphrase=\"%s\" go_dev_addr="
3177                         MACSTR "%s",
3178                         wpa_s->ifname, ssid_txt,
3179                         ssid->passphrase ? ssid->passphrase : "",
3180                         MAC2STR(go_dev_addr),
3181                         persistent ? " [PERSISTENT]" : "");
3182         }
3183
3184         if (persistent)
3185                 wpas_p2p_store_persistent_group(wpa_s->parent, ssid,
3186                                                 go_dev_addr);
3187 }
3188
3189
3190 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
3191                           u32 interval1, u32 duration2, u32 interval2)
3192 {
3193         if (wpa_s->wpa_state < WPA_ASSOCIATED ||
3194             wpa_s->current_ssid == NULL ||
3195             wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
3196                 return -1;
3197
3198         return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
3199                                 wpa_s->own_addr, wpa_s->assoc_freq,
3200                                 duration1, interval1, duration2, interval2);
3201 }
3202
3203
3204 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
3205                         unsigned int interval)
3206 {
3207         return p2p_ext_listen(wpa_s->global->p2p, period, interval);
3208 }
3209
3210
3211 void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3212                            u16 reason_code, const u8 *ie, size_t ie_len)
3213 {
3214         if (wpa_s->global->p2p_disabled)
3215                 return;
3216
3217         p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3218 }
3219
3220
3221 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3222                              u16 reason_code, const u8 *ie, size_t ie_len)
3223 {
3224         if (wpa_s->global->p2p_disabled)
3225                 return;
3226
3227         p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3228 }
3229
3230
3231 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
3232 {
3233         struct p2p_data *p2p = wpa_s->global->p2p;
3234
3235         if (p2p == NULL)
3236                 return;
3237
3238         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
3239                 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
3240
3241         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
3242                 u8 pri_dev_type[8];
3243                 if (wpa_s->conf->device_type) {
3244                         if (wps_dev_type_str2bin(wpa_s->conf->device_type,
3245                                                  pri_dev_type) < 0) {
3246                                 wpa_printf(MSG_ERROR, "P2P: Invalid "
3247                                            "device_type");
3248                         } else
3249                                 p2p_set_pri_dev_type(p2p, pri_dev_type);
3250                 }
3251         }
3252
3253         if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
3254                 u8 sec_dev_type[P2P_SEC_DEVICE_TYPES][8];
3255                 size_t num = 0;
3256                 int i;
3257                 for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
3258                         if (wpa_s->conf->sec_device_type[i] == NULL)
3259                                 continue;
3260                         if (wps_dev_type_str2bin(
3261                                     wpa_s->conf->sec_device_type[i],
3262                                     sec_dev_type[num]) < 0) {
3263                                 wpa_printf(MSG_ERROR, "P2P: Invalid "
3264                                            "sec_device_type");
3265                                 continue;
3266                         }
3267                         num++;
3268                         if (num == P2P_SEC_DEVICE_TYPES)
3269                                 break;
3270                 }
3271                 p2p_set_sec_dev_types(p2p, (void *) sec_dev_type, num);
3272         }
3273
3274         if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
3275             wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3276                 char country[3];
3277                 country[0] = wpa_s->conf->country[0];
3278                 country[1] = wpa_s->conf->country[1];
3279                 country[2] = 0x04;
3280                 p2p_set_country(p2p, country);
3281         }
3282
3283         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
3284                 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
3285                                      wpa_s->conf->p2p_ssid_postfix ?
3286                                      os_strlen(wpa_s->conf->p2p_ssid_postfix) :
3287                                      0);
3288         }
3289 }