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