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